Missing arg's for __construct still instantiates object?

Discussion of testing theory and practice, including methodologies (such as TDD, BDD, DDD, Agile, XP) and software - anything to do with testing goes here. (Formerly "The Testing Side of Development")

Moderator: General Moderators

User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

d11wtq wrote:When you don't pass args to the public constructor PHP only generates E_WARNING. Trying to access private members if E_FATAL.

* snip *

SimpleTest swallows those warning though probably.
SimpleTest does take in those errors, but doesn't swallow unless I include the expectError()'s (or use the deprecated swallowErrors() )

My point/request is just that - the error level should be, imo, E_FATAL, not E_WARNING :)
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

PHP can use "null" as the value, just like when you try to echo $foo before it's created. That's why it's not fatal. Wrong functions names, or acess to private members on the other hand means quite simply that PHP cannot let you do what you want because there's nothing for it to call.
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

Yes, I can see that, but why do we have:

Code: Select all

function ($var = 'foo') {}
if we can still just ignore required params which will have a default of null?

I'm influenced by just about every other language I've used I guess.. if the constructor (or any method/function) does not fit both the name, parameters, and state (static/not static) it dies, where as PHP is the only one to not die. :) Ergo the parameters and state declaration are just as important as the access declaration.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Because it's "wrong" to ignore the required params, but PHP being loosely typed can just set null, but not quietly. Other languages are more strictly typed (I'm going to assume you refer to java) and wouldn't physically work if you don't pass args, because as you know, the parameters are actually part of the method signature. In PHP, the method signature is just its name.
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

Not just Java, SmallTalk, VB, C based languages etc. too :)
Post Reply