访问修饰
public:公有的 全局都能访问
protected:受保护的 只能自或父类、子类访问
private:私有的 只能自己内部访问 外部不能访问
魔术方法:
(必须设置成pulbic ,而且参数不能通过引用来传递)
__construct()/__destruct():
__set()/__get()/__isset()/__unset():
__set():在类外部对私有的或者受保护的或者未定义的成员进行赋值的时候会自动调用
__get():在类外部对私有的或者受保护的或者未定义的成员进行读取的时候回自动调用
<?php
header('content-type:text/html;charset=utf-8');
/*
封装性:
1.访问修饰符
var
public:公有的 全局都能访问
protected:受保护的 只能自或父类、子类访问
private:私有的 只能自己内部访问 外部不能访问
*/
class Person{
public $username='king';
protected $age=12;
private $salary=12345678.9;
public function say(){
echo 'hello world...<br/>';
}
protected function makeMoney(){
echo '赚大钱...<br/>';
}
private function papapa(){
echo 'papapa...<br/>';
}
public function test(){
echo 'this is a test<br/>';
echo $this->age.'<br/>';
echo $this->salary.'<br/>';
}
}
$p1=new Person;
echo '用户名:'.$p1->username.'<br/>';
$p1->test();
// echo '年龄为:'.$p1->age.'<br/>';
// echo '薪水:'.$p1->salary.'<br/>';
###final关键词
-
final 关键字代表的是最终,不能被重写或者重载
-
PHP 5 新增了一个 final 关键字。如果父类中的方法被声明为 final,则子类无法覆盖该方法。如果一个类被声明为 final,则不能被继承。
<?php
header('content-type:text/html;charset=utf-8');
class Person{
public $username;
public $age;
public function __construct($username,$age){
$this->username=$username;
$this->age=$age;
echo 'Person类的构造函数<br/>';
}
public function getInfo(){
return '用户名:'.$this->username.'--年龄:'.$this->age;
}
//final 关键字代表的是最终,不能被重写或者重载
// PHP 5 新增了一个 final 关键字。如果父类中的方法被声明为 final,则子类无法覆盖该方法。如果一个类被声明为 final,则不能被继承。
final public function test(){
echo '我才是最帅的人...<br/>';
}
}
class Student extends Person{
// public $username;
// public $age;
public $school;
public function __construct($username,$age,$school){
// $this->username=$username;
// $this->age=$age;
parent::__construct($username,$age);
$this->school=$school;
echo 'Student的构造函数...<br/>';
}
public function study(){
return $this->username.'在'.$this->school.'上学<br/>';
}
public function getInfo(){
// return '用户名:'.$this->username.'--年龄:'.$this->age.'--学校:'.$this->school;
$info=parent::getInfo();
$info.='--学校:'.$this->school;
return $info;
}
}
class Teacher extends Person{
public $course;
public function __construct($username,$age,$course){
parent::__construct($username,$age);
$this->course=$course;
}
public function teach(){
return $this->username.'教'.$this->course.'<br/>';
}
public function getInfo(){
$info=parent::getInfo();
$info.='所教课程:'.$this->course;
return $info;
}
public function test(){
echo 'i am the smart boy...<br/>';
}
}
// $stu1=new Student('king',12,'北大');
// echo $stu1->study();
// echo $stu1->getInfo();
$teacher1=new Teacher('queen',33,'PHP');
echo $teacher1->teach();
echo $teacher1->getInfo();
echo '<br/>';
//final 关键字代表的是最终,不能被重写或者重载
// PHP 5 新增了一个 final 关键字。如果父类中的方法被声明为 final,则子类无法覆盖该方法。如果一个类被声明为 final,则不能被继承。
$teacher1->test();
static、const 、self关键字 静态方法 、静态变量
- 类内部声明const 常量
- 在类内部:使用self::常量访问
- 类外部使用:类名::常量
<?php
header('content-type:text/html;charset=utf-8');
class Test{
const PI=3.14;
const COUNTY='中国';
public function testConst(){
echo self::PI.'<br/>';
echo self::COUNTY.'<br/>';
echo static::PI.'<br/>';
}
}
echo Test::PI;
echo '<br/>';
echo Test::COUNTY;
echo '<br/>';
$t1=new Test;
$t1->testConst();
echo '<br/>';
// echo $t1->PI;
is_a($obj,'OBJ')
; //$obj是否属于OBJ类或者父类
($a instanceof A)
;//instanceof 用于确定一个 PHP 变量是否属于某一类 class 的实例:
get_class($a)
; //返回$a所属对象类名
get_parent_class()
:返回类或者对象的父类名