- Instantiate the class ---- Black & usage, by string to instantiate
class Coach
{
public function the __construct ()
{
echo " Welcome North Wu Tang " . " \ n- " ;
}
public function Slogan ()
{
echo " Houhou ~ ha ~ " . " \ n- " ;
}
}
$ wenwa = ' coach ' ; // string corresponding to the coach class
$ Duwa = new new $ wenwa;
$ Duwa -> Slogan ();
- Constructor
? < PHP
class mylove
{
public $ name = " Chen Peichang " ;
public function __construct ()
{
echo " like to practice fighting brother " . " \ The n- " ;
}
public function showmyname ()
{
echo " acquired the name: " . $ the this . -> name " \ n- " ;
}
}
$ CPC = new new mylove ();
$ CPC -> showmyname ();
?>
Result:
I like to practice fighting brother
acquired the name: Chen Peichang
- Inheritance (with different python, php is a single inheritance language, that is, a class can have only one parent)
Parent code as follows:
class Coach
{
public $ = Master " Zhang " ;
protected $ = Lover " D cauldron " ;
Private $ LoveGame = " SM " ;
public function Slogan ()
{
echo " Houhou ~ ~ ha " . " \ n- " ;
}
}
定义父类coach的子类cpc,继承使用关键词extends
class cpc extends coach
{
public function __construct()
{
//$this->master = $master;
echo "我的师父是".$this->master."__我爱".$this->lover."__喜欢玩"."\n";
}
public function self_introduce($name,$age)
{
echo "hello~my name is ".$name."今年芳龄".$age."\n";
}
}
$cpc = new cpc();
$cpc->slogan();
输出结果:
吼吼~哈哈~
hello~my name is 陈培昌今年芳龄21
点评:哇啊哦~这么厉害,子类cpc里没有定义slogan方法居然可以调用!正是继承的魔力之所在!