constructors

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
rathlon
Forum Commoner
Posts: 45
Joined: Sun Nov 10, 2002 8:07 pm

constructors

Post 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?
User avatar
protokol
Forum Contributor
Posts: 353
Joined: Fri Jun 21, 2002 7:00 pm
Location: Cleveland, OH
Contact:

Re: constructors

Post 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.
Post Reply