Welcome to WebmasterWorld Guest from 54.159.190.106

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

I have recently come across this; is this

declaring data types (I think...)

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

WebmasterWorld Senior Member 5+ Year Member



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
7:39 am on Sep 27, 2010 (gmt 0)
9:19 am on Sep 27, 2010 (gmt 0)

5+ Year Member



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.
10:02 am on Sep 27, 2010 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member



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.
9:10 pm on Sep 27, 2010 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member



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
 

Featured Threads

Hot Threads This Week

Hot Threads This Month