<?php
class A {
public $foo = 1;
}
$a = new A;
$b = $a; // $a ,$b都是同一个标识符的拷贝
// ($a) = ($b) = <id>
$b->foo = 2;
echo $a->foo."\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
<?php
class SubObject
{
static $instances = 0;
public $instance;
public function __construct()
{
$this->instance = ++self::$instances;
}
public function __clone()
{
$this->instance = ++self::$instances;
}
}
class MyCloneable
{
public $object1;
public $object2;
function __clone()
{
// 强制复制一份this->object, 否则仍然指向同一个对象
$this->object1 = clone $this->object1;
}
function cloneTest()
{
echo 'cloneTest';
}
}
$obj = new MyCloneable();
$obj->object1 = new SubObject();
$obj->object2 = new SubObject();
$obj2 = clone $obj;
print("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
}
}
}
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";
<?php
class A
{
private $proPrivate = "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
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有