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在面向对象部分有很多相关的魔术方法,这 ...
随机推荐
- 关于port的关闭——Linux
本文出自:http://blog.csdn.net/svitter 引文出自:http://bbs.chinaunix.net/thread-775649-1-1.html 1.关闭服务 servic ...
- 关于Service常驻内存不被清理的解决方法.
众所周知, Service是跑后台的. 但是有些Rom厂商把一键清理做的真是太好用了, 以至于一键清理变成了一种习惯, Service已经变的不再是Service了. 那为什么像诸如360, 微信, ...
- 火狐restclient
RESTClient是一款用于测试各种Web服务的插件,它可以向服务器发送各种HTTP请求(用户也可以自定义请求方式),并显示服务器响应.使用RESTClient您可以方便的测试各种Web服务,为您的 ...
- Java 8 Stream API Example Tutorial
Stream API Overview Before we look into Java 8 Stream API Examples, let’s see why it was required. S ...
- CentOS(十)--与Linux文件和目录管理相关的一些重要命令②
在结束了第二期的广交会实习之后,又迎来了几天休闲的日子,继续学习Linux.在上一篇随笔 Linux学习之CentOS(十七)--与Linux文件和目录管理相关的一些重要命令① 中,详细记录了与Lin ...
- UseAdaptiveSizePolicy与CMS垃圾回收同时使用导致的JVM报错
系统在灰度环境上变更时发现JVM启动报错,详细检查JVM配置参数,发现新境了如下配置: -XX:+UseAdaptiveSizePolicy和-XX:+UseConcMarkSweepGC 初步猜想是 ...
- 通过使用精简客户端,且不需要安装的客户端,配合PLSQL连接oracle数据库
通过使用精简客户端,且不需要安装的客户端,配合PLSQL连接oracle数据库. 首先下载安装包在Oralce官方网站上下载Oracle Instantclient Basic package.地址如 ...
- poj2243
Knight Moves Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 13433 Accepted: 7518 Des ...
- Linux 命令 - cp: 拷贝文件和目录
命令格式 cp [OPTION]... [-T] SOURCE DEST cp [OPTION]... SOURCE... DIRECTORY cp [OPTION]... -t DIRECTORY ...
- Linux 命令 - passwd: 更改用户密码
命令格式 passwd [-k] [-l] [-u [-f]] [-d] [-e] [-n mindays] [-x maxdays] [-w warndays] [-i inactiveda ...