To make the use of objects easier, PHP also provides a number of magic methods,
or special methods that are called when certain common actions occur
within objects. This allows developers to perform a number of useful
tasks with relative ease.
Using Constructors and Destructors
When an object is instantiated, it's often desirable to set a few things
right off the bat. To handle this, PHP provides the magic method __construct(), which is called automatically whenever a new object iscreated.
For the purpose of illustrating the concept of constructors, add a constructor to
MyClass that will output a message whenever a new instance of the class is created:<?phpclassMyClass{public$prop1="I'm a class property!";publicfunction__construct(){echo'The class "',__CLASS__,'" was initiated!<br />';}publicfunctionsetProperty($newval){$this->prop1 =$newval;}publicfunctiongetProperty(){return$this->prop1 ."<br />";}}// Create a new object$obj=newMyClass;// Get the value of $prop1echo$obj->getProperty();// Output a message at the end of the fileecho"End of file.<br />";?>
Note — __CLASS__ returns the name of the class in which it is called; this is what is known as a magic constant. There are several available magic constants, which you can read more about in the PHP manual.Reloading the file in your browser will produce the following result:Theclass"MyClass"was initiated!I'm aclassproperty!Endof file.
To call a function when the object is destroyed, the __destruct() magic method is available. This is useful for class cleanup (closing a database connection, for instance).Output a message when the object is destroyed by defining the magic method
__destruct() in MyClass:<?phpclassMyClass{public$prop1="I'm a class property!";publicfunction__construct(){echo'The class "',__CLASS__,'" was initiated!<br />';}publicfunction__destruct(){echo'The class "',__CLASS__,'" was destroyed.<br />';}publicfunctionsetProperty($newval){$this->prop1 =$newval;}publicfunctiongetProperty(){return$this->prop1 ."<br />";}}// Create a new object$obj=newMyClass;// Get the value of $prop1echo$obj->getProperty();// Output a message at the end of the fileecho"End of file.<br />";?>
With a destructor defined, reloading the test file results in the following output:Theclass"MyClass"was initiated!I'm aclassproperty!Endof file.
The class "MyClass" was destroyed."When the end of a file is reached, PHP automatically releases all resources."To explicitly trigger the destructor, you can destroy the object using the
function unset():<?phpclassMyClass{public$prop1="I'm a class property!";publicfunction__construct(){echo'The class "',__CLASS__,'" was initiated!<br />';}publicfunction__destruct(){echo'The class "',__CLASS__,'" was destroyed.<br />';}publicfunctionsetProperty($newval){$this->prop1 =$newval;}publicfunctiongetProperty(){return$this->prop1 ."<br />";}}// Create a new object$obj=newMyClass;// Get the value of $prop1echo$obj->getProperty();// Destroy the objectunset($obj);// Output a message at the end of the fileecho"End of file.<br />";?>
Now the result changes to the following when loaded in your browser:Theclass"MyClass"was initiated!I'm aclassproperty!Theclass"MyClass"was destroyed.Endof file.
Converting to a String
To avoid an error if a script attempts to output
MyClass as a string, another magic method is used called __toString().
Without
__toString(), attempting to output the object as a string results in a fatal error. Attempt to use echo to output the object without a magic method in place:<?phpclassMyClass{public$prop1="I'm a class property!";publicfunction__construct(){echo'The class "',__CLASS__,'" was initiated!<br />';}publicfunction__destruct(){echo'The class "',__CLASS__,'" was destroyed.<br />';}publicfunctionsetProperty($newval){$this->prop1 =$newval;}publicfunctiongetProperty(){return$this->prop1 ."<br />";}}// Create a new object$obj=newMyClass;// Output the object as a stringecho$obj;// Destroy the objectunset($obj);// Output a message at the end of the fileecho"End of file.<br />";?>
This results in the following:Theclass"MyClass"was initiated!
Catchable fatal error: Object ofclassMyClass could not be converted to string in /Applications/XAMPP/xamppfiles/htdocs/testing/test.php on line 40
To avoid this error, add a __toString() method:<?phpclassMyClass{public$prop1="I'm a class property!";publicfunction__construct(){echo'The class "',__CLASS__,'" was initiated!<br />';}publicfunction__destruct(){echo'The class "',__CLASS__,'" was destroyed.<br />';}publicfunction__toString(){echo"Using the toString method: ";return$this->getProperty();}publicfunctionsetProperty($newval){$this->prop1 =$newval;}publicfunctiongetProperty(){return$this->prop1 ."<br />";}}// Create a new object$obj=newMyClass;// Output the object as a stringecho$obj;// Destroy the objectunset($obj);// Output a message at the end of the fileecho"End of file.<br />";?>
In this case, attempting to convert the object to a string results in a call to the getProperty() method. Load the test script in your browser to see the result:Theclass"MyClass"was initiated!Using the toString method: I'm aclassproperty!Theclass"MyClass"was destroyed.Endof file.
Tip — In addition to the magic methods discussed in
this section, several others are available. For a complete list of magic
methods, see the PHP manual page.
Post a Comment
Silahkan anda tulis komentar di bawah ini !