对象传递:一种说法是“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
桃源资源网 Design By www.nqtax.com
暂无“PHP对象相关知识总结”评论...