OPP :面向对象编程,深入理解类class

类(class)是面向对象设计的基础。包含属性(变量)和方法(函数)的结构,用关键字class定义,一旦定义了一个类,就可以用关键字new创建这个类的任意数量的对象,累的属性和方法可以通过->结构访问。
比如,创建一个对象Person

class Person
{
    public $name=‘’;
    function name($newname = null){
        if( ! is_null($newname) ){
            $this->name = $newname;
        } 
        return $this->name;
    }
}

$sed = new Person;
$sed->name(‘XIAOLI’);
echo “Hello ,{$sed->name}\n”
$tc = new Person;
$tc->name(‘GONG’);
echo “Look out below {$tc->name}\n”

运行:Hello ,XIAOLI
           Look out below GONG 

猜你喜欢

转载自blog.csdn.net/li15735929289/article/details/78604110