PHP 魔术方法 __construct __destruct (一)
慢慢长寻夜,明月高空挂
__construct() - 在每次创建新对象时先调用此方法
__destruct() - 对象的所有引用都被删除或者当对象被显式销毁时执行
<?php /**
* 清晰的认识__construct() __destruct
*/
class Example { public static $link;
//在类实例化的时候自动加载__construct这个方法
public function __construct($localhost, $username, $password, $db) {
self::$link = mysql_connect($localhost, $username, $password);
if (mysql_errno()) {
die('错误:' . mysql_error());
}
mysql_set_charset('utf8');
mysql_select_db($db);
} /**
* 通过__construct链接好数据库然后执行sql语句......
*/ //当类需要被删除或者销毁这个类的时候自动加载__destruct这个方法
public function __destruct() {
echo '<pre>';
var_dump(self::$link);
mysql_close(self::$link);
var_dump(self::$link);
} } $mysql = new Example('localhost', 'root', 'root', 'test');
结果:
resource(2) of type (mysql link)
resource(2) of type (Unknown)
PHP 魔术方法 __construct __destruct (一)的更多相关文章
- PHP超全局变量、魔术变量和魔术方法
PHP超全局变量 $_GET 通过get方式传递的值(通过 URL 参数传递给当前脚本的变量的数组.) $_POST 通过post形式传递的值(当 HTTP POST 请求的 Content-Type ...
- php魔术方法小结
php魔术方法 __construct() __construct(mixed ...$values = ""): void PHP 允许开发者在一个类中定义一个方法作为构造函数. ...
- PHP中的魔术方法:__construct, __destruct , __call, __callStatic,__get, __set, __isset, __unset , __sleep, __wakeup, __toString, __set_state, __clone and __autoload
1.__get.__set 这两个方法是为在类和他们的父类中没有声明的属性而设计的: __get( $property ) 当调用一个未定义的属性时访问此方法: __set( $property, $ ...
- PHP中的魔术方法总结 :__construct, __destruct , __call, __callStatic,__get, __set, __isset, __unset , __sleep, __wakeup, __toStr
PHP中的魔术方法总结 :__construct, __destruct , __call, __callStatic,__get, __set, __isset, __unset , __sleep ...
- PHP中的11个魔术方法总结:__construct,、__destruct、__call等
PHP中的魔术方法总结 :__construct, __destruct , __call, __callStatic,__get, __set, __isset, __unset , __sleep ...
- PHP中的魔术方法总结 :__construct, __destruct , __call, __callStatic,__get, __set, __isset, __unset , __sleep
PHP中的魔术方法总结 :__construct, __destruct , __call, __callStatic,__get, __set, __isset, __unset , __sleep ...
- PHP中的魔术方法总结:__construct,__destruct ,__call,__callStatic,__get,__set,__isset, __unset ,__sleep,__wakeup,__toString,__set_state,__clone,__autoload
1.__get.__set这两个方法是为在类和他们的父类中没有声明的属性而设计的__get( $property ) 当调用一个未定义的属性时访问此方法__set( $property, $value ...
- PHP基础知识之魔术方法
__construct(), __destruct(), __call(), __callStatic(), __get(), __set(), __isset(), __unset(), __sle ...
- 前端学PHP之面向对象系列第二篇——魔术方法
× 目录 [1]构造方法 [2]析构方法 [3]不可访问属性[4]对象复制[5]字符串[6]对象不存在[7]自动加载类[8]串行化[9]函数调用 前面的话 php在面向对象部分有很多相关的魔术方法,这 ...
随机推荐
- memset用法详解(转)
问题描述: int * cow = new int[n+1]; memset(cow,0,(n+1)*4); 将N+1个元素置成0,为什么不行 memset是对一个字节的int4个字节,因此*4 但是 ...
- 在 Fedora 里安装自带的 MATE和 cinnamon
参见 http://wiki.mate-desktop.org/download#fedora安装方法: yum groupinstall mate-desktop yum groupinstall ...
- as 中的反射
flash.utils包中的getDefinitionByName,getQualifiedClassName,getQualifiedSuperclassName..可以当做反射用. 1.getDe ...
- php 常用正则表达式
判断“正浮点数”: preg_match('/^(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*) ...
- VS VC++ 设置版本号
我并不是专职的VC++的开发者,只是有时候偶尔要开发一些C++的DLL,每当要发布新版本的时候,隔得时间长一点总会忘记了在那里设置生成文件的版本号. 在这里把VC++设置的步骤记录下来,以备忘! 设置 ...
- PHP--关于模板的原理和解析
此内容用作笔记,以备日后查看,此内容为学习李炎恢课程而来,并非自己所创,如有问题请私信~ 将PHP代码和静态HTML代码进行分离,使代码的可读性和维护性得到显著提高. 使用模板引擎: 我们所说的模板是 ...
- HTML5 indexedDB数据库的入门学习(二)
上一篇关于indexedDB的学习笔记主要写了indexedDB数据库的基本操作—增删改查:但是为什么我们要用indexedDB呢?为什么indexedDB受到了开发者们的青睐呢?最主要的就是inde ...
- controller,link,compile不同
测试案例 .directive('testDirective', function() { return { restrict: 'E', template: '<p>Hello {{nu ...
- CF Spreadsheets (数学)
Spreadsheets time limit per test 10 seconds memory limit per test 64 megabytes input standard input ...
- Redis--分布式锁
在高并发的使用场景下,如何让redis里的数据尽量保持一致,可以采用分布式锁.以分布式锁的方式来保证对临界资源的互斥读写. redis使用缓存作为分布式锁,性能非常强劲,在一些不错的硬件上,redis ...