对象传递:一种说法是“PHP对象是通过引用传递的”,更准确的说法是别名(标识符)传递,即它们都保存着同一个标识符(ID)的拷贝,这个标识符指向同一个对象的真正内容。
<"\n";//2 $c = new A; $d = &$c; // $c ,$d是引用 // ($c,$d) = <id> $d->foo = 2; echo $c->foo."\n";//2 $e = new A; function foo($obj) { // ($obj) = ($e) = <id> $obj->foo = 2; } foo($e); echo $e->foo."\n";//2
"htmlcode">
<"Original Object:\n"); print_r($obj); print("Cloned Object:\n"); print_r($obj2); echo $obj2->cloneTest().":\n"; echo (new Reflectionclass($obj2));
上例输出结果:
Original Object: MyCloneable Object ( [object1] => SubObject Object ( [instance] => 1 ) [object2] => SubObject Object ( [instance] => 2 ) ) Cloned Object: MyCloneable Object ( [object1] => SubObject Object ( [instance] => 3 ) [object2] => SubObject Object ( [instance] => 2 ) ) cloneTest: Class [ <user> class MyCloneable ] { @@ /public/t.php 18-33 - Constants [0] { } - Static properties [0] { } - Static methods [0] { } - Properties [2] { Property [ <default> public $object1 ] Property [ <default> public $object2 ] } - Methods [2] { Method [ <user> public method __clone ] { @@ /public/t.php 23 - 27 } Method [ <user> public method cloneTest ] { @@ /public/t.php 29 - 32 } } }
"htmlcode">
class Foo { public static $my_static = 'foo'; public function staticValue() { return self::$my_static; } } class Bar extends Foo { public function fooStatic() { return parent::$my_static; } } print Foo::$my_static . "\n"; $foo = new Foo(); print $foo->staticValue() . "\n"; print $foo->my_static . "\n"; // Undefined "Property" my_static print $foo::$my_static . "\n"; $classname = 'Foo'; print $classname::$my_static . "\n"; // As of PHP 5.3.0 print Bar::$my_static . "\n"; $bar = new Bar(); print $bar->fooStatic() . "\n";
"htmlcode">
<"private of A"; protected $proProtected = "protected of A"; public $proPublic = "public of A"; private function foo() { echo $this->proPrivate."\n"; echo $this->proProtected."\n"; echo $this->proPublic."\n"; } public function test() { $this->foo(); static::foo(); } } class B extends A { /* foo() will be copied to B, hence its scope will still be A and * the call be successful */ } class C extends A { private $proPrivate = "private of C"; protected $proProtected = "protected of C"; public $proPublic = "public of C"; private function foo() { /* original method is replaced; the scope of the new one is C */ echo "I am C\n"; } public function myFoo() { //parent::foo(); $this->foo(); } } echo "Class B:\n"; $b = new B(); $b->test(); echo "\nClass C:\n"; $c = new C(); $c->myFoo(); $c->test(); //fails
上例输出结果:
Class B: private of A protected of A public of A private of A protected of A public of A Class C: I am C private of A protected of C public of C Fatal error: Uncaught Error: Call to private method C::foo() from context 'A' in /public/t.php:19 Stack trace: #0 /public/t.php(54): A->test() #1 {main} thrown in /public/t.php on line 19
•继承:官方文档对继承有这样一段描述“当扩展一个类,子类就会继承父类所有公有的和受保护的方法。除非子类覆盖了父类的方法,被继承的方法都会保留其原有功能”,言下之意似乎私有属性和方法不会被继承;然而上例又告诉我们子类拥有与父类一致的属性和方法,继承就是全盘复制,这才能满足我们对继承编程的需求,如果私有的不能继承,子类就必须自行重新定义,在大多数时候没有必要。另外就是可见性问题,父类的私有属性和方法在子类是不可见的。上例还告诉我们对象实际执行的域要考虑可见性、继承、后期静态绑定机制。
标签:
PHP对象,PHP,对象
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件!
如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
狼山资源网 Copyright www.pvsay.com
暂无“PHP对象相关知识总结”评论...
稳了!魔兽国服回归的3条重磅消息!官宣时间再确认!
昨天有一位朋友在大神群里分享,自己亚服账号被封号之后居然弹出了国服的封号信息对话框。
这里面让他访问的是一个国服的战网网址,com.cn和后面的zh都非常明白地表明这就是国服战网。
而他在复制这个网址并且进行登录之后,确实是网易的网址,也就是我们熟悉的停服之后国服发布的暴雪游戏产品运营到期开放退款的说明。这是一件比较奇怪的事情,因为以前都没有出现这样的情况,现在突然提示跳转到国服战网的网址,是不是说明了简体中文客户端已经开始进行更新了呢?