Different ways of instantiating class instances in PHP -
this question has reply here:
php class instantiation. utilize or not utilize parentheses? 2 answersapologies not beingness able improve articulate issue. i've tried doing simple testing , haven't been able shed much light. in case, i'm wonder what, if any, difference exists between next 2 examples:
<?php class foo { public function __construct() { } } $foo1 = new foo;
and this:
<?php class foo { public function __construct() { } } $foo1 = new foo();
note in sec illustration using parenthesis along 'new' keyword. if there not differences here, there foo class declaration create differences? , if differences exist, particular php? thanks.
i sense it's improve instantiate classes using parenthesis, if constructor doesn't need arguments. while examples same thing, wouldn't if constructor changed. it's matter of taste , style though.
here's first example:
<?php class foo { public function __construct() } $foo1 = new foo;
that works, if did this?
public function __construct($db = null) {}
.. well, still work, because arguments have default values.
but if did this?
public function __construct($db) {}
.. break due missing argument, , need parenthesis in order pass arguments. if ever went , made foo constructor more interesting, you'd have add together parenthesis along of arguments.
finally, languages do evolve. don't ever see php complaining when utilize parenthesis, could 1 day become grumpy if don't. do utilize no-parenthesis way this class we're instantiating has no constructor. it's .. personal taste. utilize absence meaningfully:
$f = new foo; // class has no constructor, or constructor has no arguments $f = new foo(); // class has constructor default arguments can set later $f = new foo($bar) // class has constructor, , needs arguments
the above can save time if done consistently throughout big code base. i'm not sure much longer, folks improve @ testing - that's opinion.
currently, - no, there's no difference between 2 given examples.
php class
No comments:
Post a Comment