php面向对象三大特性
php面向对象三大特性:多态性,封装性,继承性
多态性:
多态性是指面向不同的对象时,表现出不同的反映,比如对自己喜欢的人和不喜欢的人的态度,反映
<?php
/**
* Created by PhpStorm.
* User: SK
* Date: 2022/11/24
* Time: 17:10
*/
class a{
public function a($q){
if($q instanceof c){
$q->action();
}
}
}
abstract class c {
abstract public function action();
}
class b extends c{
public function action(){
echo 'test1';
}
}
class d extends c{
public function action(){
echo 'test2';
}
}
$a=new b();
$b=new d();
$c=new a($a);
$d=new a($b);
?>
继承和封装:
<?php
/**
* Created by PhpStorm.
* User: SK
* Date: 2022/11/24
* Time: 14:58
*/
#封装,只有内容才允许访问,不被外部所访问
class s{
public $age;
public static $name='k';
private $money=100;
public function __construct($age)
{
$this->age=$age;
}
public function test(){
echo self::$name;
}
public static function test2(){
echo '555555';
}
}
echo s::$name;
echo s::test2();
#继承性
class j extends s{
public function test(){
echo 'ffff';
}
}
$a=new j(20);
echo $a->age;
echo $a::$name;
?>