ホームページ >バックエンド開発 >PHPチュートリアル >PHP オブジェクト指向クラスでキーワード $this、static、final、const、self を使用する方法。
3 つのキーワード this、self、parent の違いは、それぞれ this、self、father を参照すると文字通り理解しやすいです。まず、いくつかの概念を確立しましょう。これらの 3 つのキーワードはどこで使用されるのでしょうか。これが現在のオブジェクトへのポインター (C のポインターを使用して見てみましょう) であり、self が現在のクラスのポインターを指すことを簡単に説明しましょう。親クラスへのポインタです。ここでの説明にポインタを頻繁に使用しますが、これはおそらく、それを表現するのにこれ以上適した言語がないためです。
<?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();//1echo '<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;"><?phpclass 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>クラス内の現在のクラスのプロパティとメソッドを呼び出すメソッドは 3 つあります。 、parent、$this の 3 つのキーワードの違いは次のとおりです。 self は現在のクラスを指すために使用され、parent は現在のクラスの親クラスを指すために使用されます。このキーワードを使用して、次のプロパティとメソッドを呼び出すことができます。親クラス; $this は、クラス本体のプロパティとメソッド内でそれ自体を呼び出すために使用されます。
<?phpclass 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
<?phpclass 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."位访客";?>結果は次のようになります:
<?phpfinal class MyClass{//此类将不允许被继承 final function fun1(){......}//此方法将不允许被重写}const 属性の宣言形式は const $PI=3.14 ではなく、const PI=3.14 であることに注意してください。
self はクラス自体を表し、現在のクラスを指します。通常、クラスの静的メンバー、メソッド、定数にアクセスするために使用されます。
PHP クラスのメンバー変数またはメソッドにアクセスするとき、参照される変数またはメソッドが const (定数を定義) または static (宣言された) として宣言されているかどうかstatic) の場合は、演算子:: を使用する必要があります。それ以外の場合、参照される変数またはメソッドが const または static として宣言されていない場合は、演算子 -> を使用する必要があります。
さらに、クラス内から const または static の変数またはメソッドにアクセスする場合は、逆に、クラス内から非 const または static の変数またはメソッドにアクセスする場合は、自己参照の self を使用する必要があります。の場合は、自己参照 $this を使用する必要があります。
二重コロン演算子は、クラス内の静的、定数、およびオーバーライドされたプロパティとメソッドにアクセスできるスコープ修飾演算子、スコープ解決演算子です。
クラス定義の外で使用する場合は、クラス名を使用して呼び出します。 PHP 5.3.0 では、クラス名の代わりに変数を使用できます。プログラム リスト: 変数を使用してクラス定義の外部にアクセスします。
<?phpclass 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; }}プログラムリスト: クラス定義外で使用::
<?phpclass Fruit { const CONST_VALUE = 'Fruit Color';}$classname = 'Fruit';echo $classname::CONST_VALUE; // As of PHP 5.3.0echo Fruit::CONST_VALUE;?>プログラム実行結果: Fruit Color Red
プログラムリスト: 親メソッドの呼び出し
<?phpclass 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"; }}プログラム実行結果:
プログラムリスト: スコープ修飾子を使用する
<?phpclass 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();?>プログラムの実行結果: カラーを表示