Page 2 of 2

Posted: Mon Feb 27, 2006 11:42 am
by Maugrim_The_Reaper
A constructor can offer a few methods of setting up an object with predefined properties. Mostly you'll want some predefined properties. Now you could just use another method in the object, but then you can't create a working object in one line, you need two - which can make object instantiation a more complex beat than is necessary.

Code: Select all

$db = new DataAccess();
$user = new User();
$user->setDB($db);

// or

$user = new User( new DataAccess() );
Or you could just have new User(), and let the constructor find the DataAccess class using a singleton instance or ServiceLocator. Contructors just make sense once you start using them - they simplify a lot of process, and allow a little flexibility. Unit testers will know all about dependency injection and its uses and alternates when testing.

Posted: Mon Feb 27, 2006 12:10 pm
by AKA Panama Jack
feyd wrote:Run something claiming compatibility with strict mode on, it's fun getting 500 "var deprecated" warnings. But I digress.
I consider that as a BUG in one of the versions of PHP 5. Especially since they are going to return the VAR to PHP 6. :) Whomever had the idea to remove VAR wasn't thinking to straight but they got back on the right track for 6.

Posted: Mon Feb 27, 2006 12:13 pm
by AKA Panama Jack
arborint wrote:For me, the advantage of "__constuct()" rather than "ClassName()" is in refactoring. Often when you create a class you get the name a little wrong. Using "__constuct()" simplifies refactoring because you don't have to rename the class and the constructor. It is one less mistake to make when renaming a class. I suppose down the road it will help tools like IDEs and class generators as well.
I don't see how unless you were silly enough to bury that function in the middle of the class. If I need to use something like this it is the first thing right after the class statement. That makes it incredibly easy to change along with the class name if needed.

Posted: Mon Feb 27, 2006 2:52 pm
by Christopher
AKA Panama Jack wrote:I don't see how unless you were silly enough to bury that function in the middle of the class. If I need to use something like this it is the first thing right after the class statement. That makes it incredibly easy to change along with the class name if needed.
For human editing of classes it is pretty trivial, but I still forget to rename the constructor occasionally. It' just an annoyance. It is too bad they used he class name originally because for backward compatablity you really can't use __construct(). But I was talking more about tools than hand editing.

Posted: Mon Feb 27, 2006 6:06 pm
by alex.barylski
feyd wrote:psst, destructors can be overloaded.
Really...care to explain how? 8)

Posted: Mon Feb 27, 2006 6:41 pm
by feyd

Code: Select all

[feyd@home]>php -r "class a{public function __destruct(){echo get_class().'::'.__FUNCTION__.'.';}} class b extends a{public function __destruct(){echo get_class().'::'.__FUNCTION__.'.';parent::__destruct();}} $b = new b(); $b = null;"
b::__destruct.a::__destruct.

Posted: Mon Feb 27, 2006 6:44 pm
by Christopher
It is important to know that they are overloaded, because you need to do the following to run the inherited constructor:

Code: Select all

function __construct() {
    parent::__contruct();
}
This give you control to use the parent constructor or not.

Posted: Mon Feb 27, 2006 6:56 pm
by alex.barylski
feyd wrote:

Code: Select all

[feyd@home]
>php -r "

class a{
  public function __destruct()
  { 
    echo get_class().'::'.__FUNCTION__.'.';
  } 
} 

class b extends a{
  public function __destruct()
  { 
     echo get_class().'::'.__FUNCTION__.'.';
     parent::__destruct();
  }
} 

$b = new b(); 
$b = null;"

b::__destruct.a::__destruct.
Interesting...but thats not overloading!!!

Overloading is: http://en.wikipedia.org/wiki/Function_o ... verloading

What you've demonstrated is actually refered to as method overriding

http://en.wikipedia.org/wiki/Method_ove ... ogramming)

Overloading requires multiple functions with the same name in the same scope, each with a unique function signature...which is impossible in dtors() like I said...they don't have parameters or return values, etc...or they shouldn't anyways.

So yes, I was correct in what I stated...in that dtors()'s cannot be overloaded...

Cheers :)

Posted: Mon Feb 27, 2006 9:56 pm
by Christopher
Well I understood what feyd meant, but now thanks to Hockey's pedantics we can seem cool if we ever meet Alan Kay, Bertrand Meyer or Niklaus Wirth at a cocktail party.

Posted: Mon Feb 27, 2006 10:13 pm
by alex.barylski
arborint wrote:Well I understood what feyd meant, but now thanks to Hockey's pedantics we can seem cool if we ever meet Alan Kay, Bertrand Meyer or Niklaus Wirth at a cocktail party.
Keep talking dude...I swear your getting smarter 8)

Posted: Tue Feb 28, 2006 1:05 pm
by timvw
<off-topic>
How much sense does it make to overload functions in a language as PHP anyway? Oh no, i remember i already started that thread once..
</off-topic>