authority finalfunctionfunction_name //访问控制关键字+关键字+function+方法名,参数列表
{
....
}
<?phpclassemp
{private$name;
private$age;
publicfunctionemp_name($e_name)
{$this->name=$e_name;
echo'My name is '.$this->name.'.';
}
publicfinalfunctionemp_age($e_age)
{$this->age=$e_age;
echo'I am '.$this->age.' years old.';
}
}
classmarextendsemp //mar类继承emp类,成功继承了final方法
{
}
$marshall=new mar();
$marshall->emp_name('Marshall');
$marshall->emp_age(21);
?><?phpclassemp
{private$name;
private$age;
publicfunctionemp_name($e_name)
{$this->name=$e_name;
echo'My name is '.$this->name.'.';
}
publicfinalfunctionemp_age($e_age)
{$this->age=$e_age;
echo'I am '.$this->age.' years old.';
}
}
classmarextendsemp
{publicfunctionemp_age($e_age) //覆盖final成员方法会报错
{$this->age=($e_age+20);
echo'I am '.$this->age.' years old.';
}
}
$marshall=new mar();
$marshall->emp_name('Marshall');
$marshall->emp_age(21);
?>
如类不想被继承,方法不想被子类重写,可以利用final关键字来修饰。
clone关键字
建立对象的副本,可以再实例化一个对象并给属性添加相同的值。
$new_name=clone$objname; //新对象名+关键字+要复制的对象名<?phpclassemp
{private$name;
private$age;
private$sex;
publicfunction__construct($name,$age,$sex)
{$this->name=$name;
$this->age=$age;
$this->sex=$sex;
}
publicfunctioninfo()
{echo'My name is '.$this->name.', I am '.$this->age.' years old, I am a '.$this->sex.'.<br/>';
}
}
$mar=new emp('Marshall',21,'boy');
$mar->info();
$john=clone$mar;
$john->info();
?>//利用__clone()方法来做复制的初始化工作<?phpclassemp
{private$name;
private$id;
publicfunction__construct($name,$id=1)
{$this->id=$id;
$this->name=$name;
}
publicfunction__clone()
{$this->id++;
}
publicfunctioninfo()
{echo'My name is '.$this->name.', My ID Number is '.$this->id.'.<br/>';
}
}
$mar=new emp('Marshall');
$mar->info();
$john=clone$mar;
$john->info();
?>
常用魔术方法
set( ),get( )方法
1.__set()
<?phpclassemp
{public$name;
private$age;
private$sex;
publicfunction__set($varname,$value)
{if($varname=='age')
{
if($value>=20&&$value<=65)
{
$this->age=$value;
}
else
{
echo'age number is not correct.';
}
}
if($varname=='sex')
{
if($value=='boy'||$value=='girl')
{
$this->sex=$value;
}
else
{
echo'the kind of sex is not correct.';
}
}
}
publicfunctioninfo()
{echo'My name is '.$this->name.', I am '.$this->age.' years old, I am a '.$this->sex.'.<br/>';
}
}
$mar=new emp();
$mar->name='Marshall';
$mar->age=21;
$mar->sex='boy';
$mar->info();
$mar->age=18;
$mar->info();
$mar->sex='other';
$mar->info();
?>
//虽然两个成员是私有的,但是也可以在类的外部赋值
2.__get()
<?phpclassemp
{private$name;
private$age;
private$sex;
publicfunction__get($varname)
{if($varname=='name')
{
echo'My name is '.$this->name.'.<br/>';
return;
}
elseif($varname=='age'||$varname=='sex')
{
echo'Secret.';
}
else
{
echo'No such a value.';
}
}
publicfunction__construct($name,$age,$sex)
{$this->name=$name;
$this->age=$age;
$this->sex=$sex;
}
}
$mar=new emp('Marshall',21,'boy');
$mar->name;
$mar->age;
$mar->sex;
$mar->school;
?>
call( ),callStatic( )方法
authority staticfunction__call($varname,$values)
{
....
}
publicstaticfunction__callStatic($varname,$values)
{
....
}
1.__call()
<?phpclassemp
{private$name;
private$id;
publicfunction__construct($name,$id=1)
{$this->id=$id;
$this->name=$name;
}
publicfunction__call($func_name,$values) //调用不存在或是不可见的成员方法时自动调用
{echo'The function which you call can not access or not exist.';
}
publicfunctioninfo()
{echo'My name is '.$this->name.', My ID Number is '.$this->id.'.<br/>';
}
privatefunctionshow_info()
{echo'Age:21';
}
}
$mar=new emp('Marshall');
$mar->info();
$mar->show_info();
?>2.__callStatic()
<?phpclassemp
{staticfunctionshow_info()
{echo'My name is Marshall.';
}
privatestaticfunctionshow_aba()
{echo'I can send Email.';
}
publicstaticfunction__callStatic($func_name,$values)
{echo'The function which you call can not access or not exist.';
}
}
emp::show_info();
emp::show_aba();
emp::hello();
?>