Blogger Information
Blog 48
fans 0
comment 0
visits 36358
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
类中静态成员与访问-2018.09.03
雨天的博客
Original
556 people have browsed it

实例

<?php
/**
 * 静态成员与访问
 * 1.静态成员:静态属性和静态方法
 * 2.定义静态成员关键字 static
 * 3.静态成员属于类,要使用类来访问
 * 4.静态属性必须用类来访问,静态方法可以使用来访问也可以使用对象访问
 * 5.静态成员与对象无关,所以不能用伪变量$this
 * 6.访问时类后面使用:范围解析符
 * 7.在类中引用自身使用self
 */

//范围解析符的作用:
//1.访问静态成员
//2.访问常量
//3.继承上下文中引用覆盖成员

class mysql
{
    //静态属性声名
    static $pdo;
    static $host = '127.0.0.1';
    static $user = 'root';
    static $pwd = 'root';
    static $dbname = 'stu';
    //静态方法声名
    static function connect()
    {
        //访问静态属性 self::
        $dsn = 'mysql:host='.self::$host.';dbname='.self::$dbname;
        self::$pdo = new PDO($dsn,self::$user,self::$pwd);

    }
    static function  select($table,$field='*',$num=5)
    {
        //访问静态属性
        $stmt = self::$pdo->prepare("select {$field} from {$table} limit {$num}");
        $stmt->execute();
        $row = $stmt->fetchAll(PDO::FETCH_ASSOC);
        return $row;
    }

}
//访问静态方法
mysql::connect();
$res = mysql::select('stuclass','name,age',5);
echo '<pre>';
var_export($res);

运行实例 »

点击 "运行实例" 按钮查看在线实例


Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!