Page 1 of 1
constructors
Posted: Mon Mar 17, 2003 11:42 am
by rathlon
I have defined a class, and the page loads and works just fine, but I didn't define a constructor. Obviously constructors aren't required to create an object. Well, let me rephrase that, user defined constructors aren't required? To create my object I simply called $home = new page(); Is that the constructor? If I don't specify a constructor in my class definition, what does the php vm use to create my object? Are constructors required for object instantiation or are they simply used to give your objects sensible starting values for the objects internal properties?
Re: constructors
Posted: Mon Mar 17, 2003 12:31 pm
by protokol
rathlon wrote:Well, let me rephrase that, user defined constructors aren't required?
Nope.
rathlon wrote:To create my object I simply called $home = new page(); Is that the constructor?
Yes, this will "construct" the new object.
rathlon wrote:If I don't specify a constructor in my class definition, what does the php vm use to create my object?
If no constructor is defined in the class, then no internal variables will be set unless you explicitly define their values:
var $my_variable = "Hello";
In this case, the constructor basically is just using those values.
rathlon wrote:Are constructors required for object instantiation or are they simply used to give your objects sensible starting values for the objects internal properties?
They are not required. Yes, they are used to set starting values in the object. Also they can be used to call a function that you want called whenever you instantiate your object.