homepage Welcome to WebmasterWorld Guest from 54.167.10.244
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
I have recently come across this; is this
declaring data types (I think...)
Matthew1980




msg:4207241
 10:31 pm on Sep 26, 2010 (gmt 0)

static function afunction(stdClass $Req, Array $Dec, Object $logged)

Is this a proper way, ie do you really need to declare stdClass within a class function? I have never seen this before, nor had the need to use this sort of thing, it's just got me thinking. I know about declaring 'Array' for only accepting array's into functions, and Objects for only objects etc, I'm not even sure where to look in the manual for this?

Advice; better methods; suggestions please?

Cheers,
MRb

 

astupidname




msg:4207370
 7:39 am on Sep 27, 2010 (gmt 0)

See Php Manual/Language Reference/Classes and Objects/Type Hinting [us2.php.net]

morehawes




msg:4207391
 9:19 am on Sep 27, 2010 (gmt 0)

I don't tend to use type hinting either - only if passing the wrong type could do some real damage which isn't usually such a big problem in PHP.

enigma1




msg:4207399
 10:02 am on Sep 27, 2010 (gmt 0)

You do not declare stdClass there, its just an argument where the function forces a type. If the type is different you suppose to have an error handler in place and do something about it. Otherwise based on the error level PHP will display the error, script and line of error something you don't want to expose with web applications.

The stdClass is a predefined class, if you do:

$class = (object)'my string';

Then the $class object has a member variable called "scalar" that contains the assigned value. So doing:
echo $class->scalar;
will output 'my string'

So when you call the function you mentioned you could do:

foo::afunction((object)'my string', .......);
You are passing a predefined class object in the function.

<?php
class foo {
static function afunction(stdClass $Req) {
echo $Req->scalar;
}
}

foo::afunction((object)'a new string');

// Error check pass a string
foo::afunction('a new string');
?>

If you pass the string directly or a different type then you will get an error from PHP that shows all kinds of details about the script (subject to the error level). So now you need to setup some error handling.

Matthew1980




msg:4207698
 9:10 pm on Sep 27, 2010 (gmt 0)

Hi all,

Thanks for all the replies, I have learned some useful things there that I had no idea about.

Though I should like to know more about Scalar in the context of php as there are a few definitions out there, but I can't find one that suits this context.

Cheers,
MRb

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved