PHP魔法方法
PHP教程 - PHP魔法方法
每当你看到一个方法名以双下划线开头,它是一个“魔法"方法。PHP保留所有以 __ 开头的方法。
PHP类构造函数
构造函数是一种特殊的方法当你创建一个实例时由PHP调用类。
PHP类构造函数具有以下语法。
public function __construct(Optional Parameter) {}
<?PHP
class MyClass {
function __construct() {
echo "I am being constructed.";
}
}
$obj = new MyClass;
?>
上面的代码生成以下结果。

类MyClass包含一个非常简单的构造函数,只显示消息。 当代码然后从该类创建一个对象,调用构造函数并显示消息。
实施例2
你可以传递参数到构造函数,像正常的方法。
<?PHP
class Person {
private $_firstName;
private $_lastName;
private $_age;
public function __construct( $firstName, $lastName, $age ) {
$this-> _firstName = $firstName;
$this-> _lastName = $lastName;
$this-> _age = $age;
}
}
$p = new Person( "James", "Bond", 28 );
?>
Person类包含三个私有属性和接受三个值的构造函数,将三个属性设置为这些值。
实施例3
如果一个类包含一个构造函数,它只有在从该类创建对象时才被调用。
如果从子类创建对象,则只调用子类的构造函数。
要使子类调用其父构造函数,调用parent :: __ construct()。
<?PHP
class NameTag {
public $Words;
}
class Book {
public $Name;
public function say() {
print "Woof!\n";
}
public function __construct($BookName) {
print "Creating a Book: $BookName\n";
$this->Name = $BookName;
}
}
class ComputerBook extends Book {
public function say() {
print "Computer!\n";
}
public function __construct($BookName) {
parent::__construct($BookName);
print "Creating a computer book\n";
}
}
?>
最好先调用 parent :: __ construct()一个子类的构造函数,以便设置所有父类的属性。
PHP类析构函数
析构函数用于在对象从内存中删除之前对其进行整理。
除了使用构造函数外,您可以使用与构造函数相同的方式创建析构函数方法__destruct()而不是__construct():
function __destruct() {
// (Clean up here)
}
删除对象时调用PHP析构函数。析构函数方法 __ destruct()不带参数。
<?PHP
class Book {
public $Name;
public function say() {
print "Woof!\n";
}
public function __construct($BookName) {
print "Creating a Book: $BookName\n";
$this->Name = $BookName;
}
public function __destruct() {
print "{$this->Name} is destructing\n";
}
}
$aBook = new Book("PHP");
?>
上面的代码生成以下结果。

调用parent :: __ construct()。
关键的区别是,你应该调用 parent :: __ destruct()本地代码的销毁。 例如:
<?PHP
public function __destruct() {
print "{$this->Name} is no more...\n";
parent::__destruct();
}
?>
下面的代码显示了如何create构造函数和析构函数。
<?php
class Counter
{
private static $count = 0;
function __construct()
{
self::$count++;
}
function __destruct()
{
self::$count--;
}
function getCount()
{
return self::$count;
}
}
//create one instance
$c = new Counter();
//print 1
print($c->getCount() . "<br>\n");
//create a second instance
$c2 = new Counter();
//print 2
print($c->getCount() . "<br>\n");
//destroy one instance
$c2 = NULL;
//print 1
print($c->getCount() . "<br>\n");
?>
上面的代码生成以下结果。

删除对象
在对象上调用 unset()会调用它的析构函数在删除对象之前。
魔法方法列表
PHP允许你创建三个“魔术"方法,你可以使用拦截属性和方法访问:
- __get() is called whenever the calling code attempts to read an invisible property of the object
- __set() is called whenever the calling code attempts to write to an invisible property of the object
- __call() is called whenever the calling code attempts to call an invisible method of the object
可见是指不存在的属性或方法或私人或保护财产或方法。
__得到()
__get()指定什么当加载未知属性时。
例如:
<?PHP
class Book {
public $Name;
public function __get($var) {
print "Attempted to retrieve $var and failed...\n";
}
}
$aBook = new Book;
print $aBook->Price;
?>
Overloading Property Accesses with __get()
<?PHP
class Car {
public function __get( $propertyName ) {
echo "The value of "$propertyName" was requested < br / > ";
return "blue";
}
}
$car = new Car;
$x = $car->color;
echo "The car"s color is $x \n";
?>
上面的代码生成以下结果。

__组()
使用__set()方法来捕获将不可见属性设置为值的尝试,请使用需要两个参数:属性名和要设置它的值。
它不需要返回值:
public function __set( $propertyName, $propertyValue ) {
// (do whatever needs to be done to set the property value)
}
设置时调用__set()魔术方法未定义属性。
<?PHP
class Book {
public function __set($var, $val) {
print("UPDATE $var = "$val";");
}
}
$book = new Book();
$book ->Price= 1.2;
?>
上面的代码生成以下结果。

__呼叫()
使用__call()来处理对类的不存在的方法的调用。该方法应该返回一个值(如果有)回调用代码:
public function __call( $methodName, $arguments ) {
// (do stuff here)
return $returnVal;
}
当调用缺少的方法时,调用__call()魔术方法。这里是一个__call()的例子:
<?PHP
class Book {
public function __call($function, $args) {
$args = implode(", ", $args);
print "Call to $function() with args "$args" failed!\n";
}
}
$aBook = new Book;
$aBook->nonExistingMethod("foo", "bar", "baz");
?>
上面的代码生成以下结果。

__toString()
__toString()允许您为对象设置字符串值。
<?PHP
class Cat {
public function __toString() {
return "This is a cat\n";
}
}
$c = new Cat;
print $c;
?>
上面的代码生成以下结果。

实施例5
下面的代码显示了如何创建__clone方法。
<?php
class ObjectTracker
{
private static $nextSerial = 0;
private $id;
private $name;
function __construct($name)
{
$this->name = $name;
$this->id = ++self::$nextSerial;
}
function __clone()
{
$this->name = "Clone of $that->name";
$this->id = ++self::$nextSerial;
}
function getId()
{
return($this->id);
}
function getName()
{
return($this->name);
}
}
$ot = new ObjectTracker("Zeev"s Object");
$ot2 = clone $ot;
//1 Zeev"s Object
print($ot->getId() . " " . $ot->getName() . "<br>");
//2 Clone of Zeev"s Object
print($ot2->getId() . " " . $ot2->getName() . "<br>");
?>