Passing null for a class-typed argument renders catchable error ?!?

Passing null for a class-typed argument renders catchable error ?!?

am 13.01.2008 03:50:33 von seaside

I have a method

>>>
function appendChildNode( AST $aNewChild ) {
...
}
<<<

where AST is a class. If I pass null, PHP renders this message:

>>>
Catchable fatal error: Argument 1 passed to AST::appendChildNode()
must be an instance of AST, null given, called in /Applications/MAMP/
htdocs/compile/includes/CParser.inc.php(517) : eval()'d code on line 1
and defined in /Applications/MAMP/htdocs/compile/includes/AST.inc.php
on line 43
<<<

Any ideas, why I can't pass a null value?

[appendChildNode() is a method declared inside the class AST]

Re: Passing null for a class-typed argument renders catchable error ?!?

am 13.01.2008 03:56:19 von Peter Pei

in php, null is not only a value but also a type, and the type hint was
violated

Re: Passing null for a class-typed argument renders catchable error

am 13.01.2008 04:00:17 von seaside

On 13 Jan., 03:56, "Peter Pei" wrote:
> in php, null is not only a value but also a type, and the type hint was
> violated

OK, thx. How do I have to pass a null 'pointer' as an object?

Re: Passing null for a class-typed argument renders catchable error ?!?

am 13.01.2008 04:00:47 von luiheidsgoeroe

On Sun, 13 Jan 2008 03:50:33 +0100, seaside wrote:

> I have a method
>
>>>>
> function appendChildNode( AST $aNewChild ) {
> ...
> }
> <<<
>
> where AST is a class. If I pass null, PHP renders this message:
>
>>>>
> Catchable fatal error: Argument 1 passed to AST::appendChildNode()
> must be an instance of AST, null given, called in /Applications/MAMP/
> htdocs/compile/includes/CParser.inc.php(517) : eval()'d code on line 1=

> and defined in /Applications/MAMP/htdocs/compile/includes/AST.inc.php
> on line 43
> <<<
>
> Any ideas, why I can't pass a null value?

For some reason:

function appendChildNode( AST $aNewChild =3D NULL ) {
....
}

.... now it will work.
-- =

Rik Wasmus

Re: Passing null for a class-typed argument renders catchable error ?!?

am 13.01.2008 04:08:24 von Peter Pei

buggy. inconsistent lang design

Re: Passing null for a class-typed argument renders catchable error

am 13.01.2008 04:15:40 von seaside

On 13 Jan., 04:00, "Rik Wasmus" wrote:
> On Sun, 13 Jan 2008 03:50:33 +0100, seaside wrote:
> > I have a method
>
> > =A0 function appendChildNode( AST $aNewChild ) {
> > =A0 ...
> > =A0 }
> > <<<
>
> > where AST is a class. If I pass null, PHP renders this message:
>
> > Catchable fatal error: Argument 1 passed to AST::appendChildNode()
> > must be an instance of AST, null given, called in /Applications/MAMP/
> > htdocs/compile/includes/CParser.inc.php(517) : eval()'d code on line 1
> > and defined in /Applications/MAMP/htdocs/compile/includes/AST.inc.php
> > on line 43
> > <<<
>
> > Any ideas, why I can't pass a null value?
>
> For some reason:
>
> function appendChildNode( AST $aNewChild =3D NULL ) {
>
> }
>

But now, I can call it 'legally' with no parameters, which I don't
want.
At least any reader would think, 'OK, passing no value is OK'.

Do we have other options?

Additionally: I'll go into details of the code soon, but does the
default value make to
exception disappear, since I passed no value at all to appendChildNode?

Re: Passing null for a class-typed argument renders catchable error ?!?

am 13.01.2008 04:32:14 von Jerry Stuckle

seaside wrote:
> On 13 Jan., 04:00, "Rik Wasmus" wrote:
>> On Sun, 13 Jan 2008 03:50:33 +0100, seaside wrote:
>>> I have a method
>>> function appendChildNode( AST $aNewChild ) {
>>> ...
>>> }
>>> <<<
>>> where AST is a class. If I pass null, PHP renders this message:
>>> Catchable fatal error: Argument 1 passed to AST::appendChildNode()
>>> must be an instance of AST, null given, called in /Applications/MAMP/
>>> htdocs/compile/includes/CParser.inc.php(517) : eval()'d code on line 1
>>> and defined in /Applications/MAMP/htdocs/compile/includes/AST.inc.php
>>> on line 43
>>> <<<
>>> Any ideas, why I can't pass a null value?
>> For some reason:
>>
>> function appendChildNode( AST $aNewChild = NULL ) {
>>
>> }
>>
>
> But now, I can call it 'legally' with no parameters, which I don't
> want.
> At least any reader would think, 'OK, passing no value is OK'.
>
> Do we have other options?
>
> Additionally: I'll go into details of the code soon, but does the
> default value make to
> exception disappear, since I passed no value at all to appendChildNode?
>

As Rik pointed out, type hinting is a somewhat inconsistent operation in
PHP. But this a new area for PHP, and I'm not sure it's been entirely
thought out yet (as seems to be with a lot of things in PHP).

Of course, you could not use type hinting. Instead, you can use
instanceof() in the function itself to ensure you have an instance of
the object. This way you can check for null, also.


--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

Re: Passing null for a class-typed argument renders catchable error

am 13.01.2008 04:38:32 von seaside

On 13 Jan., 04:32, Jerry Stuckle wrote:
> seaside wrote:
> > On 13 Jan., 04:00, "Rik Wasmus" wrote:
> >> On Sun, 13 Jan 2008 03:50:33 +0100, seaside wrote:=

> >>> I have a method
> >>> =A0 function appendChildNode( AST $aNewChild ) {
> >>> =A0 ...
> >>> =A0 }
> >>> <<<
> >>> where AST is a class. If I pass null, PHP renders this message:
> >>> Catchable fatal error: Argument 1 passed to AST::appendChildNode()
> >>> must be an instance of AST, null given, called in /Applications/MAMP/
> >>> htdocs/compile/includes/CParser.inc.php(517) : eval()'d code on line 1=

> >>> and defined in /Applications/MAMP/htdocs/compile/includes/AST.inc.php
> >>> on line 43
> >>> <<<
> >>> Any ideas, why I can't pass a null value?
> >> For some reason:
>
> >> function appendChildNode( AST $aNewChild =3D NULL ) {
>
> >> }
>
> > But now, I can call it 'legally' with no parameters, which I don't
> > want.
> > At least any reader would think, 'OK, passing no value is OK'.
>
> > Do we have other options?
>
> > Additionally: I'll go into details of the code soon, but does the
> > default value make to
> > exception disappear, since I passed no value at all to appendChildNode?
>
> As Rik pointed out, type hinting is a somewhat inconsistent operation in
> PHP. =A0But this a new area for PHP, and I'm not sure it's been entirely
> thought out yet (as seems to be with a lot of things in PHP).

Thx, Jerry! In fact, the app failed to pass a value as an argument to
appendChildNode().

I'm building a parser generator and fetched a returned value from a
non-terminal symbol
from the wrong (let's say) stack frame.

Fixed it, removed the ' =3D NULL' - and things work fine.

Thx again for all quick answers!

Re: Passing null for a class-typed argument renders catchable error ?!?

am 13.01.2008 22:58:46 von Michael Fesser

..oO(seaside)

>On 13 Jan., 04:00, "Rik Wasmus" wrote:
>
>> For some reason:
>>
>> function appendChildNode( AST $aNewChild = NULL ) {
>>
>> }
>>
>
>But now, I can call it 'legally' with no parameters, which I don't
>want.

Correct. It's the same as if you explicitly pass NULL.

>At least any reader would think, 'OK, passing no value is OK'.
>
>Do we have other options?

For what? It does exactly what you described in your original posting.

Micha

Re: Passing null for a class-typed argument renders catchable error ?!?

am 13.01.2008 22:58:46 von Michael Fesser

..oO(Peter Pei)

>buggy. inconsistent lang design

Wrong. It's not inconsistent, it's just not properly documented.
The behaviour is perfectly logical and quite useful if you know it:

public function foo(TBar $obj) {}

=> You must pass a TBar object.

public function foo(TBar $obj = NULL) {}

=> The parameter is optional, but if you pass something, it must be an
object of class TBar.

That's how I want it.

Micha

PS: Please learn to quote properly. Your posting alone taken out of
context is meaningless.

Re: Passing null for a class-typed argument renders catchable error ?!?

am 13.01.2008 23:23:15 von Peter Pei

it is absolutely inconsistent and bad design. null should assume the role of
any type, but not its own type. by saying = null, you are defining a default
value in any other normal cases. here it happens to mean something else.
the slightest inconsistent is inconsistent to me.

Re: Passing null for a class-typed argument renders catchable error ?!?

am 13.01.2008 23:25:25 von Peter Pei

You cannot see his concern does not mean there is no concern. the language
is obviously badly defined here. it is forgived, as this is an improving
area in php.

Re: Passing null for a class-typed argument renders catchable error ?!?

am 13.01.2008 23:42:26 von Michael Fesser

..oO(Peter Pei)

>it is absolutely inconsistent and bad design.

NACK

>null should assume the role of
>any type, but not its own type. by saying = null, you are defining a default
>value in any other normal cases. here it happens to mean something else.

It means exactly the same thing - nothing (= no value).

Micha

Re: Passing null for a class-typed argument renders catchable error ?!?

am 14.01.2008 00:35:07 von Peter Pei

that's what messed up your brain - nothing and no value. nothing is nothing,
no value is no value.

Re: Passing null for a class-typed argument renders catchable error ?!?

am 14.01.2008 14:37:20 von Michael Fesser

..oO(Peter Pei)

>that's what messed up your brain - nothing and no value. nothing is nothing,
>no value is no value.

NULL = no value = nothing

Micha