Re: [PHP] optional object arguments to a function
Michael A. Peters wrote: Rene Veerman wrote: On Sat, Feb 13, 2010 at 9:05 AM, Michael A. Peters mpet...@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. -- Thanks! -Shawn http://www.spidean.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] optional object arguments to a function
Shawn McKenzie wrote: Michael A. Peters wrote: Rene Veerman wrote: On Sat, Feb 13, 2010 at 9:05 AM, Michael A. Peters mpet...@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 :) -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] optional object arguments to a function
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 mpet...@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 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] optional object arguments to a function
Shawn McKenzie wrote: Michael A. Peters wrote: Rene Veerman wrote: On Sat, Feb 13, 2010 at 9:05 AM, Michael A. Peters mpet...@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. Possibly nothing - but passing an array with keys I think makes it easier to determine what the object (a DOM node) corresponds with by just looking at the key (IE for a single integral there can be lower limit, upper limit, etc.) Maybe after implementing it that way and actually getting things working, I can look into passing the dom objects as actual arguments instead of wrapping them in array. But some existing functions like tidy take some of their optional arguments in a key-value array and it is now in php core so it isn't unprecedented to use a key-value array. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] optional object arguments to a function
I've started working on a class using DOMDocument to assemble MathML in php. The class, assuming I actually succeed, will eventually be used for parsing LaTeX math equations to MathML without the need to have TeX installed. I probably won't be able to support all the possibilities for equations that LaTeX does w/o a TeX install (and definitely not user defined macros) but I suspect I can (hopefully) cover most of the common stuff. One thing I don't know how to do, though, is write a function where arguments are optional object. IE for a function to generate an integral, the limits are optional but if specified must be an object (since they may be an equation themselves). I want the default to be some kind of a null object so I know to do nothing with it if it is null. With string/integer you just do function foo($a='',$b='',$c=false) { } How do I specify a default null object, or otherwise make the argument argument optional? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] optional object arguments to a function
Op 2/13/10 8:05 AM, Michael A. Peters schreef: I've started working on a class using DOMDocument to assemble MathML in php. The class, assuming I actually succeed, will eventually be used for parsing LaTeX math equations to MathML without the need to have TeX installed. I probably won't be able to support all the possibilities for equations that LaTeX does w/o a TeX install (and definitely not user defined macros) but I suspect I can (hopefully) cover most of the common stuff. One thing I don't know how to do, though, is write a function where arguments are optional object. IE for a function to generate an integral, the limits are optional but if specified must be an object (since they may be an equation themselves). I want the default to be some kind of a null object so I know to do nothing with it if it is null. With string/integer you just do function foo($a='',$b='',$c=false) { } How do I specify a default null object, or otherwise make the argument argument optional? this first one doesn't work: ? class Foo { function dobar(stdObject $o = null) { /* ... */ } } $e = new Foo; $f = new Foo; $f-dobar();// works $f-dobar($e); // catchable fatal $f-dobar((object)array()); // catchable fatal - check the error msg!?!? ? ... but if you're able/willing to specify a user defined class then you have this option: ?php class Bar {} class Foo { function dobar(Bar $o = null) { /* ... */ } } $b = new Bar; $f = new Foo; $f-dobar($b); $f-dobar(); ? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] optional object arguments to a function
On 13 February 2010 10:07, Jochem Maas joc...@iamjochem.com wrote: Op 2/13/10 8:05 AM, Michael A. Peters schreef: I've started working on a class using DOMDocument to assemble MathML in php. The class, assuming I actually succeed, will eventually be used for parsing LaTeX math equations to MathML without the need to have TeX installed. I probably won't be able to support all the possibilities for equations that LaTeX does w/o a TeX install (and definitely not user defined macros) but I suspect I can (hopefully) cover most of the common stuff. One thing I don't know how to do, though, is write a function where arguments are optional object. IE for a function to generate an integral, the limits are optional but if specified must be an object (since they may be an equation themselves). I want the default to be some kind of a null object so I know to do nothing with it if it is null. With string/integer you just do function foo($a='',$b='',$c=false) { } How do I specify a default null object, or otherwise make the argument argument optional? this first one doesn't work: ? class Foo { function dobar(stdObject $o = null) { /* ... */ } } $e = new Foo; $f = new Foo; $f-dobar(); // works $f-dobar($e); // catchable fatal $f-dobar((object)array()); // catchable fatal - check the error msg!?!? ? ... but if you're able/willing to specify a user defined class then you have this option: ?php class Bar {} class Foo { function dobar(Bar $o = null) { /* ... */ } } $b = new Bar; $f = new Foo; $f-dobar($b); $f-dobar(); ? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php Try stdClass. If you know the class type, then that can be the type hint. You can also use func_get_args() to read all the parameters and type check them if there are MANY optional parameters. -- - Richard Quadling Standing on the shoulders of some very clever giants! EE : http://www.experts-exchange.com/M_248814.html EE4Free : http://www.experts-exchange.com/becomeAnExpert.jsp Zend Certified Engineer : http://zend.com/zce.php?c=ZEND002498r=213474731 ZOPA : http://uk.zopa.com/member/RQuadling -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] optional object arguments to a function
Op 2/13/10 8:59 PM, Richard Quadling schreef: On 13 February 2010 10:07, Jochem Maas joc...@iamjochem.com wrote: ... Try stdClass. I guess you didn't read what I wrote then. If you know the class type, then that can be the type hint. You can also use func_get_args() to read all the parameters and type check them if there are MANY optional parameters. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] optional object arguments to a function
On Sat, Feb 13, 2010 at 9:05 AM, Michael A. Peters mpet...@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. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] optional object arguments to a function
Rene Veerman wrote: On Sat, Feb 13, 2010 at 9:05 AM, Michael A. Peters mpet...@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. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php