PHP锟斤拷锟结供锟斤拷锟斤拷锟斤拷
锟斤拷overloading锟斤拷锟斤拷指锟斤拷态锟斤拷锟斤拷锟斤拷
锟斤拷锟斤拷锟皆和凤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷通锟斤拷魔锟斤拷锟斤拷锟斤拷锟斤拷magic methods锟斤拷锟斤拷实锟街的★拷
锟斤拷锟斤拷锟矫碉拷前锟斤拷锟斤拷锟斤拷未锟斤拷锟斤拷锟�锟缴硷拷锟斤拷锟斤拷锟斤拷锟皆或方凤拷时锟斤拷锟斤拷锟截凤拷锟斤拷锟结被锟斤拷锟矫★拷锟斤拷锟节猴拷锟芥将使锟斤拷锟斤拷锟缴凤拷锟斤拷锟斤拷锟皆o拷inaccessible
properties锟斤拷
锟斤拷锟斤拷锟缴凤拷锟绞凤拷锟斤拷锟斤拷inaccessible
methods锟斤拷
锟斤拷锟狡猴拷锟斤拷些未锟斤拷锟斤拷虿豢杉锟斤拷锟斤拷锟斤拷锟斤拷曰蚍椒锟斤拷锟�
锟斤拷锟叫碉拷锟斤拷锟截凤拷锟斤拷锟斤拷锟斤拷锟诫被锟斤拷锟斤拷为 public锟斤拷
Note:
锟斤拷些魔锟斤拷锟斤拷锟斤拷锟侥诧拷锟斤拷锟斤拷锟斤拷锟斤拷通锟斤拷锟斤拷锟矫达拷锟斤拷锟斤拷
Note:
PHP锟叫碉拷
锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟皆诧拷同锟斤拷锟斤拷统锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟结供锟斤拷锟酵拷锟斤拷锟斤拷喾斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷牟锟斤拷锟斤拷锟斤拷秃透锟斤拷锟斤拷锟酵拷锟�
锟芥本 | 说锟斤拷 |
---|---|
5.3.0 | 锟斤拷锟斤拷 __callStatic()魔锟斤拷锟斤拷锟斤拷锟斤拷锟缴硷拷锟斤拷未锟斤拷锟斤拷为 public 锟斤拷未锟斤拷锟斤拷为 static 锟斤拷时锟斤拷锟斤拷锟斤拷一锟斤拷锟斤拷锟芥。 |
5.1.0 | 锟斤拷锟斤拷 __isset() 锟斤拷 __unset() 锟斤拷锟斤拷魔锟斤拷锟斤拷锟斤拷锟斤拷 |
$name
) : bool$name
) : void锟节革拷锟斤拷锟缴凤拷锟斤拷锟斤拷锟皆革拷值时锟斤拷__set() 锟结被锟斤拷锟矫★拷
锟斤拷取锟斤拷锟缴凤拷锟斤拷锟斤拷锟皆碉拷值时锟斤拷__get() 锟结被锟斤拷锟矫★拷
锟斤拷锟皆诧拷锟缴凤拷锟斤拷锟斤拷锟皆碉拷锟斤拷 isset() 锟斤拷 empty() 时锟斤拷__isset() 锟结被锟斤拷锟矫★拷
锟斤拷锟皆诧拷锟缴凤拷锟斤拷锟斤拷锟皆碉拷锟斤拷 unset() 时锟斤拷__unset() 锟结被锟斤拷锟矫★拷
锟斤拷锟斤拷 $name 锟斤拷指要锟斤拷锟斤拷锟侥憋拷锟斤拷锟斤拷锟狡★拷__set() 锟斤拷锟斤拷锟斤拷 $value 锟斤拷锟斤拷指锟斤拷锟斤拷 $name 锟斤拷锟斤拷锟斤拷值锟斤拷
锟斤拷锟斤拷锟斤拷锟斤拷只锟斤拷锟节讹拷锟斤拷锟叫斤拷锟叫★拷锟节撅拷态锟斤拷锟斤拷锟叫o拷锟斤拷些魔锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟结被锟斤拷锟矫★拷锟斤拷锟斤拷锟斤拷些锟斤拷锟斤拷锟斤拷锟斤拷锟杰憋拷 锟斤拷锟斤拷为 static锟斤拷锟斤拷 PHP 5.3.0 锟斤拷, 锟斤拷锟斤拷些魔锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷为 static 锟斤拷锟斤拷锟揭伙拷锟斤拷锟斤拷妗�
Note:
锟斤拷为 PHP 锟斤拷锟斤拷值锟斤拷锟斤拷姆锟绞斤拷锟�__set() 锟侥凤拷锟斤拷值锟斤拷锟斤拷锟斤拷锟皆★拷锟斤拷锟狡碉拷, 锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷式锟斤拷值锟叫o拷__get() 锟斤拷锟结被锟斤拷锟矫o拷
$a = $obj->b = 8;
Note:
锟节筹拷 isset() 锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷越峁癸拷锟斤拷薹锟绞癸拷锟斤拷锟斤拷氐锟斤拷锟斤拷裕锟斤拷锟斤拷锟轿讹拷诺锟斤拷锟揭伙拷锟斤拷锟斤拷氐锟斤拷锟斤拷锟绞癸拷锟� empty() 时锟斤拷锟斤拷锟斤拷魔锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟结被锟斤拷锟矫★拷
为锟杰匡拷锟斤拷锟斤拷锟狡o拷锟斤拷锟诫将锟斤拷锟斤拷锟斤拷锟皆革拷值锟斤拷锟斤拷锟截憋拷锟斤拷锟斤拷使锟斤拷 empty()锟斤拷
Example #1 使锟斤拷 __get()锟斤拷__set()锟斤拷__isset() 锟斤拷 __unset() 锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷
<?php
class PropertyTest {
/** 锟斤拷锟斤拷锟截碉拷锟斤拷锟捷憋拷锟斤拷锟节达拷 */
private $data = array();
/** 锟斤拷锟截诧拷锟杰憋拷锟斤拷锟斤拷锟窖撅拷锟斤拷锟斤拷锟斤拷锟斤拷锟� */
public $declared = 1;
/** 只锟叫达拷锟斤拷锟解部锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟绞憋拷锟斤拷锟斤拷夭呕岱拷锟� */
private $hidden = 2;
public function __set($name, $value)
{
echo "Setting '$name' to '$value'\n";
$this->data[$name] = $value;
}
public function __get($name)
{
echo "Getting '$name'\n";
if (array_key_exists($name, $this->data)) {
return $this->data[$name];
}
$trace = debug_backtrace();
trigger_error(
'Undefined property via __get(): ' . $name .
' in ' . $trace[0]['file'] .
' on line ' . $trace[0]['line'],
E_USER_NOTICE);
return null;
}
/** PHP 5.1.0之锟斤拷姹� */
public function __isset($name)
{
echo "Is '$name' set?\n";
return isset($this->data[$name]);
}
/** PHP 5.1.0之锟斤拷姹� */
public function __unset($name)
{
echo "Unsetting '$name'\n";
unset($this->data[$name]);
}
/** 锟斤拷魔锟斤拷锟斤拷锟斤拷 */
public function getHidden()
{
return $this->hidden;
}
}
echo "<pre>\n";
$obj = new PropertyTest;
$obj->a = 1;
echo $obj->a . "\n\n";
var_dump(isset($obj->a));
unset($obj->a);
var_dump(isset($obj->a));
echo "\n";
echo $obj->declared . "\n\n";
echo "Let's experiment with the private property named 'hidden':\n";
echo "Privates are visible inside the class, so __get() not used...\n";
echo $obj->getHidden() . "\n";
echo "Privates not visible outside of class, so __get() is used...\n";
echo $obj->hidden . "\n";
?>
锟斤拷锟斤拷锟斤拷锟教伙拷锟斤拷锟斤拷锟�
Setting 'a' to '1' Getting 'a' 1 Is 'a' set? bool(true) Unsetting 'a' Is 'a' set? bool(false) 1 Let's experiment with the private property named 'hidden': Privates are visible inside the class, so __get() not used... 2 Privates not visible outside of class, so __get() is used... Getting 'hidden' Notice: Undefined property via __get(): hidden in <file> on line 70 in <file> on line 29
锟节讹拷锟斤拷锟叫碉拷锟斤拷一锟斤拷锟斤拷锟缴凤拷锟绞凤拷锟斤拷时锟斤拷__call() 锟结被锟斤拷锟矫★拷
锟节撅拷态锟斤拷锟斤拷锟斤拷锟叫碉拷锟斤拷一锟斤拷锟斤拷锟缴凤拷锟绞凤拷锟斤拷时锟斤拷__callStatic() 锟结被锟斤拷锟矫★拷
$name 锟斤拷锟斤拷锟斤拷要锟斤拷锟矫的凤拷锟斤拷锟斤拷锟狡★拷$arguments 锟斤拷锟斤拷锟斤拷一锟斤拷枚锟斤拷锟斤拷锟介,锟斤拷锟斤拷锟斤拷要锟斤拷锟捷革拷锟斤拷锟斤拷 $name 锟侥诧拷锟斤拷锟斤拷
Example #2 使锟斤拷 __call() 锟斤拷 __callStatic() 锟皆凤拷锟斤拷锟斤拷锟斤拷
<?php
class MethodTest
{
public function __call($name, $arguments)
{
// 注锟斤拷: $name 锟斤拷值锟斤拷锟街达拷小写
echo "Calling object method '$name' "
. implode(', ', $arguments). "\n";
}
/** PHP 5.3.0之锟斤拷姹� */
public static function __callStatic($name, $arguments)
{
// 注锟斤拷: $name 锟斤拷值锟斤拷锟街达拷小写
echo "Calling static method '$name' "
. implode(', ', $arguments). "\n";
}
}
$obj = new MethodTest;
$obj->runTest('in object context');
MethodTest::runTest('in static context'); // PHP 5.3.0之锟斤拷姹�
?>
锟斤拷锟斤拷锟斤拷锟教伙拷锟斤拷锟斤拷锟�
Calling object method 'runTest' in object context Calling static method 'runTest' in static context