Home > php教程 > php手册 > 转 PHP

转 PHP

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-06-06 19:40:07
Original
990 people have browsed it

PHP _construct _destory _call _get _set等 PHP 技术 2009-12-23 22:40:08 阅读 129 评论 0 字号: 大 中 小 1,__construct() 当实例化一个对象的时候,这个对象的这个方法首先被调用。 class Test { function __construct() { echo "before"; } } $t = ne

PHP _construct _destory _call _get _set等

PHP 技术 2009-12-23 22:40:08 阅读129 评论0 字号:

1,__construct() 当实例化一个对象的时候,这个对象的这个方法首先被调用。

class Test

{

function __construct()

{

echo "before";

}

}

$t = new Test();

输出是:

start

 

 

class Test

{

    function Test()

{

echo "end2";

}

function __construct()

{

echo "end";

}

}

$t = new Test();

//输出end

?>

我们知道 php5对象模型

2,__destruct() 当删除一个对象或对象操作终止的时候,调用该方法。

class Test

{

function __destruct()

{

echo "end";

}

}

$t = new Test();

将会输出

end

我们就可以在对象操作结束的时候进行释放资源之类的操作

3,__get() 当试图读取一个并不存在的属性的时候被调用。

如果试图读取一个对象并不存在的属性的时候,PHP就会给出错误信息。如果在类里添加__get方 法,并且我们可以用这个函数 实现类似java中反射的各种操作。

class Test

{

public function __get($key)

{

echo $key . " 不存在";

}

}

$t = new Test();

echo $t->name;

就会输出:

name 不存在

4,__set() 当试图向一个并不存在的属性写入值的时候被调用。

class Test

{

public function __set($key,$value)

{

echo '对'.$key . "附值".$value;

}

}

$t = new Test();

$t->name = "aninggo";

就会输出:

对 name 附值 aninggo

5,__call() 当试图调用一个对象并不存在的方法时,调用该方法。

class Test

{

public function __call($Key, $Args)

{

echo "您要调用的 {$Key} 方法不存在。你传入的参数是:" . print_r($Args, true);

}

}

$t = new Test();

$t->getName(aning,go);

程序将会输出:

您要调用的 getName 方法不存在。参数是:Array

(

[0] => aning

[1] => go

)

6,__toString() 当打印一个对象的时候被调用

这个方法类似于java的toString方法,当 我们直接打印对象的时候回调用这个函数

class Test

{

public function __toString()

{

return "打印 Test";

}

}

$t = new Test();

echo $t;

运行echo $t;的时候,就会调用$t->__toString();从而输出

打印 Test

7,__clone() 当对象被克隆时,被调用

class Test

{

public function __clone()

{

PHP _construct _destory _call _get _set等

计 算机技术   2009-08-03 11:52   阅读111   评论0  

字号: 大大  中中  小小

1,__construct() 当实例化一个对象的时候,这个对象的这个方法首先被调用。

class Test

{

function __construct()

{

echo "before";

}

}

$t = new Test();

输出是:

start

 

 

class Test

{

    function Test()

{

echo "end2";

}

function __construct()

{

echo "end";

}

}

$t = new Test();

//输出end

?>

我们知道 php5对象模型

2,__destruct() 当删除一个对象或对象操作终止的时候,调用该方法。

class Test

{

function __destruct()

{

echo "end";

}

}

$t = new Test();

将会输出

end

我们就可以在对象操作结束的时候进行释放资源之类的操作

3,__get() 当试图读取一个并不存在的属性的时候被调用。

如果试图读取一个对象并不存在的属性的时候,PHP就会给出错误信息。如果在类里添加__get方 法,并且我们可以用这个函数 实现类似java中反射的各种操作。

class Test

{

public function __get($key)

{

echo $key . " 不存在";

}

}

$t = new Test();

echo $t->name;

就会输出:

name 不存在

4,__set() 当试图向一个并不存在的属性写入值的时候被调用。

class Test

{

public function __set($key,$value)

{

echo '对'.$key . "附值".$value;

}

}

$t = new Test();

$t->name = "aninggo";

就会输出:

对 name 附值 aninggo

5,__call() 当试图调用一个对象并不存在的方法时,调用该方法。

class Test

{

public function __call($Key, $Args)

{

echo "您要调用的 {$Key} 方法不存在。你传入的参数是:" . print_r($Args, true);

}

}

$t = new Test();

$t->getName(aning,go);

程序将会输出:

您要调用的 getName 方法不存在。参数是:Array

(

[0] => aning

[1] => go

)

6,__toString() 当打印一个对象的时候被调用

这个方法类似于java的toString方法,当 我们直接打印对象的时候回调用这个函数

class Test

{

public function __toString()

{

return "打印 Test";

}

}

$t = new Test();

echo $t;

运行echo $t;的时候,就会调用$t->__toString();从而输出

打印 Test

7,__clone() 当对象被克隆时,被调用

class Test

{

public function __clone()

{

echo "我被复制了!";

}

}

$t = new Test();

$t1 = clone $t;

程序输出:

我被克 隆了!

echo "我被复制了!";

}

}

$t = new Test();

$t1 = clone $t;

程序输出:

我被克隆了!

Related labels:
php
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Issues
php data acquisition?
From 1970-01-01 08:00:00
0
0
0
PHP extension intl
From 1970-01-01 08:00:00
0
0
0
How to learn php well
From 1970-01-01 08:00:00
0
0
0
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template