Author Topic: Object Oriented Programming in PHP - Constructors  (Read 219 times)

0 Members and 1 Guest are viewing this topic.

Offline JusticeLeague

  • Sr. Member
  • *
  • Posts: 370
  • Karma: +1/-0
    • View Profile
Object Oriented Programming in PHP - Constructors
« on: August 31, 2008, 11:09:30 PM »
Object Oriented Programming in PHP - Constructors

(Page 3 of 7 )

You might define constructors in your classes; constructors are methods with the same name as the class and are called when you create an object of the class, for example:


class Something {
var $x;

function Something($y) {

function setX($v) {

function getX() {
return $this->x;


So you can create an object using:

$obj=new Something(6);

...and the constructor automatically assigns 6 to the data member x.

Constructors and methods are normal PHP functions so you can use default arguments.

function Something($x="3",$y="5");


$obj=new Something(); // x=3 and y=5
$obj=new Something(8); // x=8 and y=5
$obj=new Something(8,9); // x=8 and y=9

Default arguments are used in the C++ way so you can\'t pass a value to Y and let X take the default value. Arguments are assigned from left to right and when no more arguments are found if the function expected more they take the default values.

When an object of a derived class is created only its constructor is called the constructor of the Parent class is not called.

This is a gotcha of PHP because constructor chaining is a classic feature of OOP, if you want to call the base class constructor you have to do it explicitly from the derived class constructor.

It works because all methods of the parent class are available at the derived class due to inheritance.


function Another() {
$this->Something(); //explicit call to base class constructor.


Abstract Classes

A nice mechanism in OOP is the use of Abstract Classes; abstract classes are classes that cannot be instantiated and the only purpose is to define an interface for its derived classes.

Designers often use Abstract classes to force programmers to derive classes from certain base classes, so they can be certain that the new classes have some desired functionality.

There\'s no standard way to do that in PHP but, if you do need this feature, just define the base class and put a "die" call in its constructor so you can be sure that the base class is never instantiated.

Now define the methods (interface) putting "die" statements in each one so if in a derived class a programmer doesn\'t override the method then an error is raised.

Furthermore you might need to be sure, since PHP has no types, that some object is from a class derived from your base class, then add a method in the base class to identify the class (return "some id") and verify this when you receive an object as an argument.

Of course this doesn\'t work if the evil programmer overrides the method in the derived class but generally the problem is dealing with lazy programmers not evil ones! Of course it’s better to keep the base class unreachable from the programmers, just print the interface and make them work!

There\'re no destructors in PHP.

Techronnati | where technology never sleeps

Object Oriented Programming in PHP - Constructors
« on: August 31, 2008, 11:09:30 PM »

Mountain View


Related Topics

  Subject / Started by Replies Last post
0 Replies
Last post August 31, 2008, 11:06:03 PM
by JusticeLeague
1 Replies
Last post October 13, 2014, 02:47:23 AM
by MoneyRepublic
0 Replies
Last post August 31, 2008, 11:11:47 PM
by JusticeLeague
0 Replies
Last post August 31, 2008, 11:20:10 PM
by JusticeLeague
0 Replies
Last post November 27, 2008, 03:35:09 PM
by Corps

Posting Disclaimer: Any individual may post a message in this forum and may do so anonymously. Therefore, the sole author is exclusively and entirely responsible for all opinions in that message. They do not represent the official opinions of Techronnati, its administrators or moderators or the Techronnati Management. Techronnati is merely acting as an impartial conduit for constitutionally protected free speech and is not responsible and will not be held liable for the content of such messages. All images and service logos are trademarks of their respective owners.