重载
属性重载与方法重载
PHP所提供的"重载"(overloading)是指动态地"创建"类属性和方法。我们是通过魔术方法(magic methods)来实现的。
当调用当前环境下未定义或不可见的类属性或方法时,重载方法会被调用。
95Shop可以免费下载使用,是一款仿醉品商城网店系统,内置SEO优化,具有模块丰富、管理简洁直观,操作易用等特点,系统功能完整,运行速度较快,采用ASP.NET(C#)技术开发,配合SQL Serve2000数据库存储数据,运行环境为微软ASP.NET 2.0。95Shop官方网站定期开发新功能和维护升级。可以放心使用! 安装运行方法 1、下载软件压缩包; 2、将下载的软件压缩包解压缩,得到we
0
立即学习“PHP免费学习笔记(深入)”;
所有的重载方法都必须被声明为 public。
立即学习“PHP免费学习笔记(深入)”;
public void __set ( string $name , mixed $value )
public mixed __get ( string $name )
public bool __isset ( string $name )
public void __unset ( string $name )
在给不可访问属性赋值时,__set() 会被调用。
读取不可访问属性的值时,__get() 会被调用。
当对不可访问属性调用 isset() 或 empty() 时,__isset() 会被调用。
当对不可访问属性调用 unset() 时,__unset() 会被调用。
参数 $name 是指要操作的变量名称。__set() 方法的 $value 参数指定了 $name 变量的值。
属性重载只能在对象中进行。在静态方法中,这些魔术方法将不会被调用。所以这些方法都不能被 声明为 static。从 PHP 5.3.0 起, 将这些魔术方法定义为 static 会产生一个警告。
示例:
class a{ public function __set($name,$value){ //$this->data[$name]=$value; $this->$name=$value; } public function __get($name){ return $this->$name; } public function __isset($name){ return isset($this->$name); } public function __unset($name){ unset($this->$name); }}$obj_a=new a();$obj_a->first='first';echo $obj_a->first."<br>";echo isset($obj_a->second)?'second exist<br>':'second not exist<br>';echo isset($obj_a->first)?'first exist<br>':'first not exist<br>';echo empty($obj_a->first)?'first exist<br>':'first not exist<br>';unset($obj_a->first);echo isset($obj_a->first)?'first exist<br>':'first not exist<br>';结果:
first
second not exist
first exist
first not exist
first not exist
立即学习“PHP免费学习笔记(深入)”;
立即学习“PHP免费学习笔记(深入)”;
立即学习“PHP免费学习笔记(深入)”;
立即学习“PHP免费学习笔记(深入)”;
立即学习“PHP免费学习笔记(深入)”;
public mixed __call ( string $name , array $arguments )
public static mixed __callStatic ( string $name , array $arguments )
在对象中调用一个不可访问方法时,__call() 会被调用。
用静态方式中调用一个不可访问方法时,__callStatic() 会被调用。
$name 参数是要调用的方法名称。$arguments 参数是一个枚举数组,包含着要传递给方法 $name 的参数。
立即学习“PHP免费学习笔记(深入)”;
立即学习“PHP免费学习笔记(深入)”;
示例:
echo "<pre class="brush:php;toolbar:false;">";class a { public function __call($name,$arguments){ if($name=='mycall'){ print_r($arguments); call_user_func('mycall',$arguments[0],$arguments[1]); }else{ echo "call uknow function"; } }}function mycall($a1,$a2){ echo "this is mycall<br>"; print_r($a1); print_r($a2); echo "end of mycall<br>";}$obj_a=new a();$obj_a->mycall(['a'=>'aaa'],['a2'=>'a2']);$obj_a->notcall();结果:
Array( [0] => Array ( [a] => aaa ) [1] => Array ( [a2] => a2 ))this is mycallArray( [a] => aaa)Array( [a2] => a2)end of mycallcall uknow function
示例:
echo "<pre class="brush:php;toolbar:false;">";class a { public static function __callStatic($name,$arguments){ if($name=='mycall'){ print_r($arguments); call_user_func('mycall',$arguments[0],$arguments[1]); }else{ echo "call uknow function"; } }}function mycall($a1,$a2){ echo "this is mycall<br>"; print_r($a1); print_r($a2); echo "end of mycall<br>";}$obj_a=new a();$obj_a::mycall(['a'=>'aaa'],['a2'=>'a2']);$obj_a::notcall();结果:
Array( [0] => Array ( [a] => aaa ) [1] => Array ( [a2] => a2 ))this is mycallArray( [a] => aaa)Array( [a2] => a2)end of mycallcall uknow function
立即学习“PHP免费学习笔记(深入)”;
立即学习“PHP免费学习笔记(深入)”;
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号