Blogger Information
Blog 37
fans 0
comment 0
visits 21165
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
PHP编程第十二课:php基础12-PHP培训九期线上班
渡劫小能手
Original
452 people have browsed it

一、 创建类、实例化、命名空间、类成员、类方法(手写)

实例

<?php
namespace test;
class A{
    public $name = '摄像头';
    public $price = '700';
    public function chifan(){
        echo '我要吃饭';
    }
    public function heshui(){
        echo $this -> name . '我要喝水';
    }
    public function ziji(){
        $b = new self();
        echo $b -> price;
    }
}
$a = new A();
echo $a -> name;
echo $a -> price;
$a -> color = '红色';
echo '<hr/>';
$a ->ziji();
echo '<hr/>';
$a ->heshui();
//get_class_vars()是把类的成员列出来
//print_r( get_class_vars(A::class));
echo '<hr/>';
//get_class_methods()把类的方法都列出来
print_r(get_class_methods(A::class));

运行实例 »

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

2019-11-29_234115.jpg

二、构造方法


实例

<?php
class A{
    public $name;
    public $age;
    public function __construct($name,$age)
    {
        $this->name=$name;
        $this->age=$age;
        echo $this->name . $this->age;
    }
}
//当类实例化后就会立即执行构造方法
new A('乔峰','20岁');

运行实例 »

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

2019-11-29_234126.jpg

Correcting teacher:天蓬老师天蓬老师

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