php 魔法函数解释

news/2024/7/4 0:53:02 标签: php, function, class, java, 引擎
class="baidu_pl">
class="article_content clearfix">
class="htmledit_views">
class="entry-content" style="font-size: 14px;">

(1)__construct() 是PHP内置的构造函数, 是同PHP 解析引擎自动调用的, 当实例化一个对象的时候,这个对象的这个方法首先被调用。

例:class Test

{

class="tags" href="/tags/FUNCTION.html" title=function>function __construct()

{

echo “This is __construct class="tags" href="/tags/FUNCTION.html" title=function>function!”;

}

class="tags" href="/tags/FUNCTION.html" title=function>function Test()

{

echo “This is Test!”;

}

}

$objTest = new Test;      // 运行结果是“This is __construct class="tags" href="/tags/FUNCTION.html" title=function>function!”

(2)__destory()是PHP内置的析构函数,当删除一个对象或对象操作终止的时候,调用该方法,所以可进行释放资源之类的操作。

class Test

{

class="tags" href="/tags/FUNCTION.html" title=function>function __destory()

{

echo “This is __destory class="tags" href="/tags/FUNCTION.html" title=function>function!”;

}

}

$objTest = new Test;       // 运行结果是“This is __destory class="tags" href="/tags/FUNCTION.html" title=function>function!”

(3)__get()当试图读取一个并不存在的属性 的时候被调用,类似java中反射的各种操作。

class Test

{

class="tags" href="/tags/FUNCTION.html" title=function>function __get($key)

{

echo $key, “doesn’t exist!”;

}

}

$objTest = new Test;

$objTest->Name;    // 运行结果是“Name does’nt exist!”

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

class Test

{

class="tags" href="/tags/FUNCTION.html" title=function>function __set($key, $val)

{

echo “Can’t assign/”” . $val . “/” to “. $key;

}

}

$objTest = new Test;

$objTest->Name = “ljlwill”;    // 运行结果是“Can’t assign “ljlwill” to Name”

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

class Test

{

class="tags" href="/tags/FUNCTION.html" title=function>function __call($key, $args)

{

echo “The class="tags" href="/tags/FUNCTION.html" title=function>function /”". $key .”/” doesn’t exist. it’s args are “. print_r($args);

}

}

$objTest = new Test;

$objTest->getName(“2004″, “ljlwill”);

// 运行结果是 The class="tags" href="/tags/FUNCTION.html" title=function>function “getName” doesn’t exist. it’s args are: Array(

[0] => 2004;

[1] => ljlwill;

)

(6)__toString() 当打印一个对象的时候被调用,类似于java的toString方法,当我们直接打印对象的时候回调用这个函数。

class Test

{

class="tags" href="/tags/FUNCTION.html" title=function>function __toString()

{

return “This is Test!”;

}

}

$objTest = new Test;

eho $objTest;         // 运行结果是“This is Test!”

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

class Test

{

class="tags" href="/tags/FUNCTION.html" title=function>function __clone()

{

echo “I am cloned!” ;
}

}

$objTest = new Test;

$objCloneTest = clone $objTest;     // 运行结果是“I am cloned!”


http://www.niftyadmin.cn/n/761404.html

相关文章

php单列模式

首先我们要知道明确单例模式这个概念,那么什么是单例模式呢? 单例模式顾名思义,就是只有一个实例。 作为对象的创建模式, 单例模式确保某一个类只有一个实例,而且自行实例化并向整个系统提供这个实例, 这个…

PHP单例模式

相信很多朋友利用PHP在进行Web开发时都会用到设计模式,其中单例模式应该是应用最多的模式之一,本文并不讨论PHP的各种设计模式,而是重点和大家一起来分析、探讨、分享我自己对PHP单例模式的通俗认识和理解,旨在让大家彻底认清PHP单…

memcache的increment用法

PHP在操作Memcache数据的时候,我们习惯用get,set,delete比较多些,突然发现increment和decrement其实是很有用的,听说使用得当对程序的性能是很有帮助的。 先看下文档里的说明:Memcache::increment()将指定元…

memacache 函数详解

memcache函数所有的方法列表如下: Memcache::add – 添加一个值,如果已经存在,则返回false Memcache::addServer – 添加一个可供使用的服务器地址 Memcache::close – 关闭一个Memcache对象 Memcache::connect – 创建一个Memcache对象 memc…

http://tech.idv2.com/2009/03/05/use-utf8-in-download-filename/

http://tech.idv2.com/2009/03/05/use-utf8-in-download-filename/

图像…….因其本身有错无法显示

使用GD画图的代码大致如下所示 <? $imImageCreate(300,200) or die("cannot create image"); $blackImageColorAllocate($im,0,0,0); $whiteImageColorAllocate($im,255,255,255); Imagefill($im,0,0,$white); ImageString($im,5,4,10,"…

php缓存使用

<?php ob_start(); phpinfo(); $phpinfo ob_get_contents(); //文件读写操作 ob_clean(); $f fopen( "test.htm", "w "); fwrite($f,$phpinfo); fclose($f); ob_end_flush — Flush (send) the output buffer and turn off output buffering …

PHP上传文件时,文件过大$_FILES为空

在php中判断上传文件的大小&#xff0c;但是文件一但过大&#xff0c;print_r($_FILES);的值就变为null了,有时候大家会遇到这么一个问题&#xff0c;上传小文件时&#xff0c;PHP能正常获取到&#xff0c;但是文件一超过8M就变为空了&#xff0c;我在做项目的时候&#xff0c;…