Blogger Information
Blog 18
fans 0
comment 0
visits 9927
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
类的实例化与属性重载2018年9月3日作业
吕小布的博客
Original
628 people have browsed it

实例

<?php
class demo{
    public $name='小布';
}
$demo=new demo();
$demo->name='小小布';
$demo->sex='男';
$demo->dingyi=function (){
    return '自定义类方法';
};
echo $demo->name,':',$demo->sex,'<br>';

echo call_user_func($demo->dingyi);

运行实例 »

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

实例

<?php
class demo1
{
    const SITE_NAME='做作业';
    private $name;
    private $course;
    private $grade;
    //构造方法
    public function __construct($name,$course,$grade)
    {
        $this->name=$name;
        $this->course = $course;
        $this->grade = $grade;
        echo $this->show();
    }
    //输出属性内容
    public function show()
    {
        return $this->name.'的《'.$this->course.'》课程的成绩是: '.$this->grade.'分!';
    }
    //获取
    public function __get($name)
    {
        if ($name=='grade'){
            return $name.'不允许查看';
        }
        return $this->$name;
    }
    //设置
    public function __set($name, $value)
    {
        if ($name == 'grade') {
            echo $name.'不允许修改','<br>';
        }
        $this->$name = $value;
    }
    //检测
    public function __isset($name)
    {
        if ($name == 'grade') {
            return false;
        }
        return isset($this->$name);
    }
    //销毁
    public function __unset($name)
    {
        if ($name == 'grade' || $name == 'name') {
            return false;
        }
        unset($this->$name);
    }

}

echo '名称:',demo1::SITE_NAME,'<br>';
$demo1=new demo1('小布','PHP',100);
echo '<hr>';
echo $demo1->show();
echo '姓名:',$demo1->name,'<br>';
echo '成绩:',$demo1->name,'<br>';
$demo1->course='C++';
echo '课程: ', $demo1->course,'<br>';
echo isset($demo1->name) ? '存在<br>' : '<span style="color:red">不存在</span><br>';
unset($demo1->course);
echo $demo1->course,'<br>';

运行实例 »

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

实例

<?php
class Demo3
{
    //父类属性
    public $name;
    protected $age;
    private $salary;
    const APP_NAME='学生管理系统';
    //父类构造器
    public function __construct($name,$age)
    {
        $this->name=$name;
        $this->age=$age;
    }
    //属性重载
    public function __get($name)
    {
       if(isset($this->$name)){
           return $this->$name;
       }
       return '非法属性';
    }
}
class Demo3_1 extends Demo3
{
    //子类属性
    private $sex;
    const APP_NAME='教师管理系统';
    //子类将父类同名方法进行重写
    public function __construct($name,$age,$sex='男')
    {
        parent::__construct($name,$age);
        $this->sex=$sex;

    }
    public function __get($name)
    {
        if (isset($this->$name)) {
            return $this->$name;
        }
        return '非法属性';
    }
}
$demo3_1=new Demo3('小李飞刀','60');
echo $demo3_1->name.'<br>';
echo $demo3_1->age.'<br>';
echo $demo3_1->salary.'<br>';
echo Demo3_1::APP_NAME.'<br>';
echo $demo3_1->sex;

运行实例 »

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

实例

class Demo4
{
	public static $pdo = null;
	protected static $db = 
	[
		'type' => 'mysql',
		'host' => 'localhost',
		'dbname' => 'edu',
		'user' => 'root',
		'pass' => 'root',
	];

	public static function connect()
	{

		$dsn = self::$db['type'].':host='.self::$db['host'].';dbname='.self::$db['dbname'];
		self::$pdo = new PDO($dsn,self::$db['user'],self::$db['pass']);
	}
}
Demo4::connect();

运行实例 »

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


Correction status:qualified

Teacher's comments:
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