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.

<?php
class MyClass
{
  public $prop1 = "I'm a class property!";
  public function __construct()
  {
      echo 'The class "', __CLASS__, '" was initiated!<br />';
  }
  public function setProperty($newval)
  {
      $this->prop1 = $newval;
  }
  public function getProperty()
  {
      return $this->prop1 . "<br />";
  }
}
// Create a new object
$obj = new MyClass;
// Get the value of $prop1
echo $obj->getProperty();
// Output a message at the end of the file
echo "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:
The class "MyClass" was initiated!
I'm a class property!
End of 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:
<?php
class MyClass
{
  public $prop1 = "I'm a class property!";
  public function __construct()
  {
      echo 'The class "', __CLASS__, '" was initiated!<br />';
  }
  public function __destruct()
  {
      echo 'The class "', __CLASS__, '" was destroyed.<br />';
  }
  public function setProperty($newval)
  {
      $this->prop1 = $newval;
  }
  public function getProperty()
  {
      return $this->prop1 . "<br />";
  }
}
// Create a new object
$obj = new MyClass;
// Get the value of $prop1
echo $obj->getProperty();
// Output a message at the end of the file
echo "End of file.<br />";
?>
With a destructor defined, reloading the test file results in the following output:
The class "MyClass" was initiated!
I'm a class property!
End of 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():
<?php
class MyClass
{
  public $prop1 = "I'm a class property!";
  public function __construct()
  {
      echo 'The class "', __CLASS__, '" was initiated!<br />';
  }
  public function __destruct()
  {
      echo 'The class "', __CLASS__, '" was destroyed.<br />';
  }
  public function setProperty($newval)
  {
      $this->prop1 = $newval;
  }
  public function getProperty()
  {
      return $this->prop1 . "<br />";
  }
}
// Create a new object
$obj = new MyClass;
// Get the value of $prop1
echo $obj->getProperty();
// Destroy the object
unset($obj);
// Output a message at the end of the file
echo "End of file.<br />";
?>
Now the result changes to the following when loaded in your browser:
The class "MyClass" was initiated!
I'm a class property!
The class "MyClass" was destroyed.
End of file.

Converting to a String

<?php
class MyClass
{
  public $prop1 = "I'm a class property!";
  public function __construct()
  {
      echo 'The class "', __CLASS__, '" was initiated!<br />';
  }
  public function __destruct()
  {
      echo 'The class "', __CLASS__, '" was destroyed.<br />';
  }
  public function setProperty($newval)
  {
      $this->prop1 = $newval;
  }
  public function getProperty()
  {
      return $this->prop1 . "<br />";
  }
}
// Create a new object
$obj = new MyClass;
// Output the object as a string
echo $obj;
// Destroy the object
unset($obj);
// Output a message at the end of the file
echo "End of file.<br />";
?>
This results in the following:
The class "MyClass" was initiated!
Catchable fatal error: Object of class MyClass 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:
<?php
class MyClass
{
  public $prop1 = "I'm a class property!";
  public function __construct()
  {
      echo 'The class "', __CLASS__, '" was initiated!<br />';
  }
  public function __destruct()
  {
      echo 'The class "', __CLASS__, '" was destroyed.<br />';
  }
  public function __toString()
  {
      echo "Using the toString method: ";
      return $this->getProperty();
  }
  public function setProperty($newval)
  {
      $this->prop1 = $newval;
  }
  public function getProperty()
  {
      return $this->prop1 . "<br />";
  }
}
// Create a new object
$obj = new MyClass;
// Output the object as a string
echo $obj;
// Destroy the object
unset($obj);
// Output a message at the end of the file
echo "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:
The class "MyClass" was initiated!
Using the toString method: I'm a class property!
The class "MyClass" was destroyed.
End of 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

 
Top