php中类和对象的操作

在类中用$this指代对象本身。

用self::指代类本身。

 

$p1 = new Person('michael');//向Person类的构造函数__construct中传名字

echo($p1->name);//获取对象p1的public实例属性name(注意没有$)

$p1->speak();//调用对象p1的实例方法speak

 


echo(Person::$sex);//获取类变量(static $sex)

echo(Person::sex);//获取类中的常量(不是static const,没有static const。只有类变量没有类常量。)

Person::run();//调用类方法(或叫做静态方法 static function)

 

总结:

  对象实例操作用  ->

  类操作用 ::

posted on 2016-10-17 21:40  Sweet小马  阅读(999)  评论(1编辑  收藏  举报

导航