optional object arguments to a function

optional object arguments to a function

am 13.02.2010 09:05:22 von Michael Peters

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: optional object arguments to a function

am 13.02.2010 11:07:15 von Jochem Maas

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:


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: optional object arguments to a function

am 13.02.2010 21:59:48 von Richard Quadling

On 13 February 2010 10:07, Jochem Maas 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=3D'',$b=3D'',$c=3Dfalse) {
>>   }
>>
>> 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 =3D null) { /* ...=
*/ }
> }
>
> $e =3D new Foo;
> $f =3D new Foo;
>
> $f->dobar();                 =C2=
=A0  // 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:
>
> >
> class Bar {}
>
> class Foo
> {
>        function dobar(Bar $o =3D null) { /* ... */ }
> }
>
> $b =3D new Bar;
> $f =3D 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.

--=20
-----
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=3DZEND002498&r=3D213474=
731
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: optional object arguments to a function

am 14.02.2010 02:32:18 von Jochem Maas

Op 2/13/10 8:59 PM, Richard Quadling schreef:
> On 13 February 2010 10:07, Jochem Maas 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: optional object arguments to a function

am 14.02.2010 08:48:33 von Rene Veerman

On Sat, Feb 13, 2010 at 9:05 AM, Michael A. Peters 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: optional object arguments to a function

am 14.02.2010 08:53:33 von Michael Peters

Rene Veerman wrote:
> On Sat, Feb 13, 2010 at 9:05 AM, Michael A. Peters 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

Re: optional object arguments to a function

am 14.02.2010 15:40:46 von Shawn McKenzie

Michael A. Peters wrote:
> Rene Veerman wrote:
>> On Sat, Feb 13, 2010 at 9:05 AM, Michael A. Peters
>> 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: optional object arguments to a function

am 14.02.2010 18:21:47 von Nathan Rixham

Shawn McKenzie wrote:
> Michael A. Peters wrote:
>> Rene Veerman wrote:
>>> On Sat, Feb 13, 2010 at 9:05 AM, Michael A. Peters
>>> 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:

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 )

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: optional object arguments to a function

am 14.02.2010 19:19:47 von Shawn McKenzie

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
>>>> 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:
>
> > 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 )
>
> 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: optional object arguments to a function

am 14.02.2010 23:46:28 von Michael Peters

Shawn McKenzie wrote:
> Michael A. Peters wrote:
>> Rene Veerman wrote:
>>> On Sat, Feb 13, 2010 at 9:05 AM, Michael A. Peters
>>> 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