慢慢长寻夜,明月高空挂
__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 class="brush:php;toolbar:false;">';
var_dump(self::$link);
mysql_close(self::$link);
var_dump(self::$link);
}
}
$mysql = new Example('localhost', 'root', 'root', 'test');
结果:
本书图文并茂,详细讲解了使用LAMP(PHP)脚本语言开发动态Web程序的方法,如架设WAMP平台,安装与配置开源Moodle平台,PHP程序设计技术,开发用户注册与验证模块,架设LAMP平台。 本书适合计算机及其相关专业本、专科学生作为学习LAMP(PHP)程序设计或动态Web编程的教材使用,也适合对动态Web编程感兴趣的读者自觉使用,对LAMP(PHP)程序设计人员也具有一定的参考价值。
713
resource(2) of type (mysql link) resource(2) of type (Unknown)
立即学习“PHP免费学习笔记(深入)”;
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号