php-ood关键字-138

<?php

//final 关键字,定义类是类不能被继承,定义方法方法不能被重载

//self 关键字,可以在实例化前访问类的内部成员self::类的内部成员/方法/属性)=(类名::类的内部成员/方法/属性))

//static 关键字,独占内存,禁止静态方法出现非静态内容(eg.this关键字)

//const 关键字,仅定义成员属性的,属性不能使用$符合,一般大写常量。

class mypc{

public$name='mypc';

function power(){

echo"mypc isopening...";

   }

}

class my extends mypc{

function power(){

echo mypc::power()."!!!";

   }

}

$p=new my();

$p->power();

?>

http://localhost/php/22.php

mypc is opening...!!!


---Final关键字

<?php

class mypc{

public$name='mypc';

finalfunction power(){

echo"mypc isopening...";

   }

}

class my extends mypc{

function power(){

echo mypc::power()."!!!";

   }

}

$p=new my();

$p->power();

?>

http://localhost/php/22.php

Fatal error:Cannot override final method mypc::power() in C:\wamp\php\php\22.php online 16

<?php

finalclass mypc{

public$name='mypc';

function power(){

echo"mypc isopening...";

   }

}

class my extends mypc{

function power(){

echo mypc::power()."!!!";

   }

}

$p=new my();

$p->power();

?>

http://localhost/php/22.php

Fatal error:Class my may not inherit from final class (mypc) in C:\wamp\php\php\22.phpon line 16

---self关键字

<?php

classmypc{

static$name='mypc';

function power(){

//静态方法不可以出现this关键字

      self::$name="herpc";

echo self::$name." is opening...";

   }

}

class my extendsmypc{

function power(){

echomypc::power()."!!!";

   }

}

$p=new my();

my::$name="ourspc";

$p->power();

?>

http://localhost/php/22.php

herpc is opening...!!!

---CONST关键字

<?php

classmypc{

const NAME='mypc';

function power(){

echo self::NAME." isopening...";

   }

}

$p=newmypc();

$p->power();

?>

http://localhost/php/22.php

mypc is opening...


你可能感兴趣的:(PHP)