Re: optional object arguments to a function
| From: | Shawn McKenzie | Date: | Sun, 14 Feb 2010 18:19:47 +0000 |
| Subject: | Re: optional object arguments to a function | ||
| References: | 1 2 3 4 5 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-302088@lists.php.net to get a copy of this message | ||
Nathan Rixham wrote:
> Shawn McKenzie wrote:
>> Michael A. Peters wrote:
>>> Rene Veerman wrote:
>>>> On Sat, Feb 13, 2010 at 9:05 AM, Michael A. Peters <mpeters@mac.com>
>>>> wrote:
>>>>> How do I specify a default null object, or otherwise make the argument
>>>>> argument optional?
>>>>>
>>>> To my knowledge: can't be done.
>>>>
>>>> But you can check any args through the func_get_arg*() functions, then
>>>> per-parameter push 'm through a check function that checks if their
>>>> primary properties are set.
>>>> It's equivalent to checking for null ( / bad) objects.
>>>>
>>> Thank you to everybody. I think I will see how far I can get with
>>> func_get_arg - it may solve the problem.
>>>
>>> The other hackish solution I thought of is to put the object arguments
>>> into a key/value array and pass the array as a single argument to the
>>> function. That way I can check for the key and if the key is set, grab
>>> the object associated with it.
>> Maybe I mis-read your post, but what's wrong with Jochem's method.
>> That's what I was going to propose.
>>
>
> This is a problem with php; you can't do the following (since object
> isn't a class):
> function test( object $o = null )
>
> so normally you'd do:
> function test( stdClass $o = null )
>
> but this only works for stdClass - (object)"something" and *not*
> instances of classes:
>
> <?php
> class Foo {}
> $o = new Foo();
> test( $foo );
> ?>
>
> will fail because Foo is not an instance of stdClass
>
> in short there is no way (in PHP) to type hint that something should be
> an object of any class.
>
> thus you have two options to work around this; option 1:
>
> check yourself:
> function test( $o = null ) {
> if( $o !== null && !is_object($o) ) {
> throw new InvalidArgumentException( '$o must be an object' );
> }
> }
>
> ensure you always only use instances of classes and not just
> "objects/stdClass" (or make everything extend stdClass <stupid>)
>
> back to the main question - How do I specify a default null object -
> like this:
>
> function foo($a='',$b='',$c=false, $o=null) {
> if( $o !== null && !is_object($o) ) {
> throw new InvalidArgumentException( '$o must be an object' );
> }
> // in the same way a you'd do
> if( !is_string($a) ) {
> throw new InvalidArgumentException( '$a must be a string' );
> }
> }
>
> side note: if you're finding you may need an unknown number of arguments
> then other than refactoring all your design to handle one argument at a
> time to avoid cross cutting concerns, then you're stuck with
> func_get_arg and checking each argument as you go; not strict but if it
> works and it's fast..
>
> feel like I've just typed "blah blah blah" for the last 10 minutes, ahh
> well ho hum!
>
> regards :)
I guess I missed the part where he wasn't going to know what class the
object was from. I would think you would normally know.
function foo($a = '', $b = '', $c = false, myClass $o = null)
--
Thanks!
-Shawn
http://www.spidean.com