命名空间(namespace):解决全局成员的命名冲突问题
两种方式:
Namespace demo;
Namespace demo{代码段}
类:使用关键字class定义,
对象:把类实例化,使用关键字new
对象属性:也叫成员变量,定义在类中,声明时要加上访问限定符,public protected private
对象方法:定义在类中的方法,特殊:self(当前类),$this(当前实例化对象的引用)
构造方法:用来初始化对象成员,在类实例化时会被自动调用,使用__construct
不使用构造方法:
<?php
namespace demo;
header('content-type:text/html;charset=utf-8');
class Clothes{
public $name = '衣服';
public $price = '120元';
public function info1(){
$clo = new self();
return 'info1:'.$clo->name.','.$clo->price;
}
public function info2(){
return 'info2:'.$this->name.','.$this->price;
}
}
$clothes = new Clothes();
$clothes->color = '红色';
echo $clothes->color.'的'.$clothes->name.'价值'.$clothes->price;
echo '<br>';
echo $clothes->info1();
echo '<br>';
echo $clothes->info2();
?>
使用构造方法:
<?php
namespace admin;
header('content-type:text/html;charset=utf-8');
class Clothes{
public $name;
public $price;
public function __construct($name,$price){
$this->name = $name;
$this->price = $price;
}
}
$clo = new Clothes('牛子裤','150元');
echo $clo->name.','.$clo->price;
?>