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)

  

时间: 2024-12-29 20:40:58

PHP 魔术方法 __construct __destruct (一)的相关文章

php 魔术方法总结(持续更新)

类中的魔术方法 PHP 魔术方法指的是在某些时刻会自动被调用的内置函数,它们以两个连续的下划线开头. 类中的魔术方法 __construct() 类的构造函数,用于初始化对象,在对象实例化时自动运行 __destruct() 析构函数,用于在 php 运行终止时,释放对象所占用的内存.析构函数是 php 的垃圾回收机制,使用栈结构,后进先出. 构造函数和析构函数的例子 class computer{     private $brand;     function __construct($br

PHP魔术方法和魔术变量

PHP魔术方法和魔术变量 魔术方法: PHP把所有以__(两个下划线)开头的类方法当成魔术方法; __construct()->构造方法: __destruct()->析构函数; __get(string $name)->是访问和设置类不存在的成员变量时调用的: __set(string $name, mixed $value)->则是访问和设置类不存在的成员变量时调用的; __call(string $name, array $arguments)->当调用类中不存在的方法

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 and __autoload 1.__get.__set这两个方法是为在类和他们的父类中没有声明的属性而设计的__get( $property ) 当调用一个未定义的属性时访问此方法__set( $proper

PHP中的魔术方法:__construct, __destruct , __call, __callStatic,__get, __set, __isset, __unset , __sleep, __wakeup, __toString, __set_state, __clone and __autoload

1.__get.__set 这两个方法是为在类和他们的父类中没有声明的属性而设计的: __get( $property ) 当调用一个未定义的属性时访问此方法: __set( $property, $value ) 给一个未定义的属性赋值时调用: 这里的没有声明包括当使用对象调用时,访问控制为proteced,private的属性(即没有权限访问的属性). 2.__isset.__unset __isset( $property ) 当在一个未定义的属性上调用isset()函数时调用此方法: _

PHP中的魔术方法:__construct, __destruct , __call,__get, __set, __isset, __unset , __toString, __set,__clone and __autoload

1.__get.__set 这两个方法是为在类和他们的父类中没有声明的属性而设计的: __get( $property ) 当调用一个未定义的属性时访问此方法: __set( $property, $value ) 给一个未定义的属性赋值时调用: 这里的没有声明包括当使用对象调用时,访问控制为proteced,private的属性(即没有权限访问的属性). 2.__isset.__unset __isset( $property ) 当在一个未定义的属性上调用isset()函数时调用此方法: _

phpz中的魔术方法__call、__set、__get、__sleep、__invoke、__autoload、__construct、__destruct、__clone、__tostring、__callstatic

PHP5.3新增了一个叫做__invoke的魔术方法,这样在创建实例后,可以直接调用对象. class testClass { public function __invoke { print “hello world”; } } $n = new testClass; $n(); 执行结果为: hello world. 官方示例 class CallableClass { public function __invoke($x) { var_dump($x); } } $obj = new C

PHP中的魔术方法总结 :__construct, __destruct , __call, __callStatic,__get, __set, __isset, __unset , __sleep, __wakeup, __toStr

PHP中的魔术方法总结 :__construct, __destruct , __call, __callStatic,__get, __set, __isset, __unset , __sleep, __wakeup, __toStr PHP中的魔术方法总结 :__construct, __destruct , __call, __callStatic,__get, __set, __isset, __unset , __sleep, __wakeup, __toString, __set_

【转】PHP中的魔术方法总结 :__construct, __destruct , __call, __callStatic,__get, __set, __isset, __unset , __sleep, __wakeup, __toStr

PHP中的魔术方法总结 :__construct, __destruct , __call, __callStatic,__get, __set, __isset, __unset , __sleep, __wakeup, __toString, __set_state, __clone and __autoload 1.__get.__set这两个方法是为在类和他们的父类中没有声明的属性而设计的__get( $property ) 当调用一个未定义的属性时访问此方法__set( $proper

PHP中的11个魔术方法总结:__construct,、__destruct、__call等

PHP中的魔术方法总结 :__construct, __destruct , __call, __callStatic,__get, __set, __isset, __unset , __sleep, __wakeup, __toString, __set_state, __clone and __autoload 1.__get.__set这两个方法是为在类和他们的父类中没有声明的属性而设计的__get( $property ) 当调用一个未定义的属性时访问此方法__set( $proper