标签:php this self parent php面向对象关键字的使用
<?php
<span style="color:#ff0000;">// this是指向当前对象的指针</span>
class test_this{
private $content; //定义变量
function __construct($content){ //定义构造函数
$this->content= $content;
}
function __destruct(){}//定义析构函数
function printContent(){//定义打印函数
echo $this->content.'<br />';
}
}
$test=new test_this('北京欢迎你!'); //实例化对象
$test->printContent();//北京欢迎你!
$test=new test_this('新年新气象!');//再一次实例化对象
$test->printContent();//新年新气象!
echo '<br />';
<span style="color:#ff0000;">//self是指向类的本身,只跟类有关,跟任何对象实例无关</span>
class test_self{
private static $first_count; //定义静态变量
private $last_count;
function __construct(){
$this->last_count=++self::$first_count;//直接用self调用变量的值赋值给另一个变量
}
function __destruct(){}
function print_self(){
print($this->last_count);
}
}
$abc=new test_self();//实例化对象
$abc->print_self();//1
echo '<br />';
<span style="color:#ff0000;">//parent是指向父类的指针</span>
class test_parent{ //基类
public $name; //定义姓名 父类成员需要定义为public,才能够在继承类中直接使用 this来调用。
function __construct($name){
$this->name=$name;
}
}
class test_son extends test_parent{ //派生类 继承test_parent
public $gender;//定义性别
public $age; //定义年龄
function __construct($gender,$age){ //继承类的构造函数
parent::__construct('nostop');//使用parent调用父类的构造函数,来进行对父类的实例化
$this->gender=$gender;
$this->age=$age;
}
function __destruct(){}
function print_info(){
echo $this->name.'是个'.$this->gender.',今年'.$this->age.'岁'.'<br />';
}
}
$nostop=new test_son('女性','22');//实例化test_son对象
$nostop->print_info();//执行输出函数 nostop是个女性,今年23岁
?>
<span style="font-size:14px;"><?php
class MyClass{
private $name;
public function __construct($name){
$this->name=$name;
}
public function getname(){
return $this->name;
}
public function printName(){
echo $this->getname();
}
}
$myclass= new MyClass("I Like PHP");
$myclass->printName();//输出:I Like PHP
?></span>在类里面调用当前类的属性和方法有三种方法,分别是self、parent、$this,这三个关键字的区别是:self用来指向当前的类;parent用于指向当前类的父类,可以使用该关键字调用父类的属性和方法;$this用来在类体内调用自身的属性和方法。<?php
class MyObject{
public static $myStaticVar=0;
function myMethod(){
self::$myStaticVar+=2;
echo self::$myStaticVar;
}
}
$instance1=new MyObject();
$instance1->myMethod();
$instance2=new MyObject();
$instance2->myMethod();
//结果将分别打印2、4<?php
class Book{
static $num=0;
public function showMe(){
echo"您是滴".self::$num."位访客";
self::$num++;
}
}
$book1=new Book();
$book1->showMe();
echo"<br>";
$book2=new Book();
$book2->showMe();
echo"<br>";
echo"您是滴".Book::$num."位访客";
?>结果将是:<?php
final class MyClass{//此类将不允许被继承
final function fun1(){......}//此方法将不允许被重写
}
<?php
class clss_a{
private static $name="static class_a";
const PI=3.14;
public $value;
public static function getName(){
return self::$name;
}
//这种写法有误,静态方法不能访问非静态属性
public static function getName2(){
return self::$value;
}
public function getPI(){
return self::PI;
}
}注意const属性的申明格式是const PI=3.14,而不是const $PI=3.14。<?php
class Fruit {
const CONST_VALUE = 'Fruit Color';
}
$classname = 'Fruit';
echo $classname::CONST_VALUE; // As of PHP 5.3.0
echo Fruit::CONST_VALUE;
?>Program List:在类定义外部使用::<?php
class Fruit {
const CONST_VALUE = 'Fruit Color';
}
class Apple extends Fruit
{
public static $color = 'Red';
public static function doubleColon() {
echo parent::CONST_VALUE . "\n";
echo self::$color . "\n";
}
}程序运行结果:Fruit Color Red<?php
class Fruit
{
protected function showColor() {
echo "Fruit::showColor()\n";
}
}
class Apple extends Fruit
{
// Override parent's definition
public function showColor()
{
// But still call the parent function
parent::showColor();
echo "Apple::showColor()\n";
}
}
$apple = new Apple();
$apple->showColor();
?>程序运行结果:
<?php
class Apple
{
public function showColor()
{
return $this->color;
}
}
class Banana
{
public $color;
public function __construct()
{
$this->color = "Banana is yellow";
}
public function GetColor()
{
return Apple::showColor();
}
}
$banana = new Banana;
echo $banana->GetColor();
?>程序运行结果:Banana is yellow
<?php
class Fruit
{
static function color()
{
return "color";
}
static function showColor()
{
echo "show " . self::color();
}
}
class Apple extends Fruit
{
static function color()
{
return "red";
}
}
Apple::showColor();
// output is "show color"!
?>程序运行结果:show colorphp面向对象类中的$this,static,final,const,self这几个关键字使用方法。
标签:php this self parent php面向对象关键字的使用
原文地址:http://blog.csdn.net/tham_/article/details/42041929