PHP Programming/Special Methods
From Wikibooks, the open-content textbooks collection
[edit] Constructors
A constructor is a special method inside a class that is called when the object is initiated. A constructor could be used as follows:
This is the PHP5 Version:
<?php
class test {
public $name;
public function __construct ($name) {
$this->name = $name;
}
}
$testing = new test('Hello');
echo $testing->name; // Prints 'Hello'
?>
This is the equivalent PHP4.x Version: (Note that there are no public/private keywords, and the name of the class is the constructor)
<?php
class test {
var $name;
function test ($name) {
$this->name = $name;
}
}
$testing = new test('Hello');
echo $testing->name; // Prints 'Hello'
?>
In PHP5 a constructor must be declared as public or it will not work. The name of a constructor must always be __construct in PHP5.The name of the class must be used as a constructor in PHP4.
[edit] Destructors
Destructors delete an instance of an object. The destructor is declared within the object's class, and contains other code to be executed at the time of destruction:
<?php
class myClass {
function __construct() {
print "Constructing new myClass...\n";
$this->name = "My class";
}
function __destruct() {
print "Destroying " . $this->name . "\n";
}
}
$obj = new myClass();
?>
Note that destructors only exist in PHP5.
[edit] Why Constructors and Destructors Are Great
Why are constructors and destructors useful? Sometimes objects represent complex entities that use other resources or have other side effects, even though they appear as simple variables in your program. In these cases, special setup may be required when you create the object; you can use the constructor to do that automatically for you. Also, it can be very important to free those resources at the end of your program so that they don't get tied up from multiple runs or pageviews; the destructor can automatically handle that so you don't forget.
Here's an example that makes handling MySQL databases slightly simpler:
<?php
class db_link {
private $link;
public function __construct ($database_name) {
$link = mysql_connect ("localhost", "your_user_name", "your_password");
mysql_select_db ($database_name, $link);
}
function query ($sql_query) {
$result = mysql_query ($sql_query, $link);
return $result;
}
function __destruct() {
mysql_close ($link);
}
}
$db = new db_link ("MyDB")
$result = $db->query ("Select * from MyTable")
?>
The class "db_link" uses 3 functions: the constructor, which automatically logs into the database for me whenever I create a new "db_link" object, a "query" function, which I can use to get records from the database, and the destructor, which automatically closes the database whenever PhP is finished with my object instance. I could do the same things as the constructor and destructor by writing special "open" and "close" functions for the database, but then I would have to call those functions every time. This way, all I have to do is make objects and use them; they can open and close themselves automatically.