Table of Contents
php面向对象的简单总结 $this $parent self
Home php教程 php手册 php面向对象的简单总结 $this $parent self

php面向对象的简单总结 $this $parent self

Jun 13, 2016 am 09:19 AM
object

php面向对象的简单总结 $this $parent self

 虽然接触php比较长时间,但有时在使用一些基础东西的时候还会有些不确定,有些疑惑。面向对象涉及到的比较多,大概总结整理一下php的属性、对象,以及访问方式$this  $parent  self  的使用场景。

 

1. PHP类属性定义和访问方式:

 

 

 1

 2 class testClass {

 3     const tConst = 1;

 4     public $tVar = 2;    //或 public $tVar;  前面需要有变量修饰符

 5     static $tSta = 3;

 6 

 7     public function __construct(){

 8         echo $this->tConst;    //无错误,无输出

 9         echo self::tConst;    //正确输出 1

10         

11         echo $this->tVar;    // 注意tVar前不能有$符号

12         echo self::$tVar;    // Access to undeclared static property: testClass::$tVar

13         echo self::tVar;    // tVar前需要加上$符号,Undefined class constant 'tVar'

14         

15         echo $this->tSta;    //无错误,无输出

16         echo self::$tSta;    //正确输出 3

17         

18     }

19 }

20 

21 $otestClass = new testClass();

 

 

 

 

 

总结几点:

 

在实例化对象时  $otestClass = new testClass(); 其中testClass()中的()可以省略,当构造函数有显式声明需要参数时,需要在这里传入参数

如果被引用的变量或者方法被声明成const(定义常量)或者static(声明静态),那么就必须使用操作符::

如果被引用的变量或者方法没有被声明成const或者static,那么就必须使用操作符->

从类的内部访问const或者static变量或者方法,使用自引用的self

从类的内部访问不为const或者static变量或者方法,使用自引用的$this

2. $this->  究竟指向哪?

 

 1 class testClass {

 2     var $nick = '';

 3     

 4     public function __construct($nick){

 5         $this->nick = $nick;

 6     }

 7     

 8     public function display(){

 9         echo $this->nick;

10     }

11 }

12 

13 $otestClass1 = new testClass('frank');

14 $otestClass1->display();        //echo $otestClass1->nick;  和此结果相同

 

 

 

上面例子中,$this->display();   其实就是 $otestClass1->nick,因此$this究竟指向哪是由所实例化的对象决定的,指向当前对象实例的指针。包括变量、方法都是如此

 

$this->方法() 的例子:

 

 

 1 class baseClass{

 2     public function testFunc(){

 3         echo "\n" . 'I`m boss';

 4     }

 5 }

 6 

 7 class parentClass extends baseClass{

 8     public function testFunc(){

 9         echo "\n" . 'I`m the up';

10     }

11 }

12 

13 class testClass extends parentClass{

14     var $nick = '';

15     

16     public function __construct($nick){

17         $this->nick = $nick;

18     }

19     

20     public function display(){

21         echo $this->nick;

22         $this->testFunc();

23     }

24 }

25 

26 $otestClass1 = new testClass('frank');

27 $otestClass1->display();

 

 

 

这样的代码最后的输出结果是什么呢?关键是看testFunc()方法。

 

如果在类中用$this调用一个当前类中不存在的方法或变量,它会依次去父类寻找,直到找不到再报错

基于第一条,如果找到了需要的方法或变量,就会停止寻找,如果其上级父类中还有同样的,则选择当前找到的

所以上面代码输出为:

 

frank

I`m the up

 

 

3. 关于parent::

 

parent是对父类的引用

 

 

 1

 2 class A {

 3     public  $a = 'aa';

 4     public function callFunc(){

 5         echo 'parent';

 6     }

 7 }

 8 

 9 class B extends A{

10     public function __construct(){

11         parent::callFunc();

12         echo "\n" . $this->a;

13     }

14 }

15 

16 $oB = new B;

 

比如,上面的代码中,在class B中,若要调用其父类A中的callFunc()方法,就需要使用parent::callFunc(),但A类中此方法必须是public修饰的,否则会提示 Fatal error: Call to private method A::callfunc() from context 'B'...

 

另外需要注意的是,对于父类中的属性$a,调用的时候用$this,用parent就访问不到了。若$a在A类中是这样定义的:public static $a,则访问的时候就需要parent::$a

 

注意,parent不具备传递性,它只能代表当前类的父类,若此例子A类继承base类,在base类中定义baseFunc()方法,那么在B类中使用parent::baseFunc()是错误的,只能在A类中这样使用。

 

4. self::又指向哪里?

 

简单的说,self和某具体实例没有关系,它只指向当前类,不会受某具体类对象的影响

 

 

 1 class testClass{

 2     public static $count = 0;

 3     public $num = 0;

 4     

 5     function __construct(){

 6         self::$count++;

 7         $this->num++;

 8     }

 9     

10     function display(){

11         echo self::$count . "\n";

12         echo $this->num . "\n";

13     }

14 }

15 

16 $oTest1 = new testClass;

17 $oTest1->display();        //输出: 1  1

18 

19 $oTest2 = new testClass;

20 $oTest2->display();        //输出: 2  1

 

 

上面例子中self::$cout始终指向该类本身,而$num是单独存在于各个具体实例中的。

 

 

 

5. 总结:

 

  $this 指向当前的实例

 

  $parent 是父类的引用

 

  self 对当前类本身的一个引用

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Convert an array or object to a JSON string using PHP's json_encode() function Convert an array or object to a JSON string using PHP's json_encode() function Nov 03, 2023 pm 03:30 PM

JSON (JavaScriptObjectNotation) is a lightweight data exchange format that has become a common format for data exchange between web applications. PHP's json_encode() function can convert an array or object into a JSON string. This article will introduce how to use PHP's json_encode() function, including syntax, parameters, return values, and specific examples. Syntax The syntax of the json_encode() function is as follows: st

Source code exploration: How are objects called in Python? Source code exploration: How are objects called in Python? May 11, 2023 am 11:46 AM

Wedge We know that objects are created in two main ways, one is through Python/CAPI, and the other is by calling a type object. For instance objects of built-in types, both methods are supported. For example, lists can be created through [] or list(). The former is Python/CAPI and the latter is a calling type object. But for instance objects of custom classes, we can only create them by calling type objects. If an object can be called, then the object is callable, otherwise it is not callable. Determining whether an object is callable depends on whether a method is defined in its corresponding type object. like

How to convert MySQL query result array to object? How to convert MySQL query result array to object? Apr 29, 2024 pm 01:09 PM

Here's how to convert a MySQL query result array into an object: Create an empty object array. Loop through the resulting array and create a new object for each row. Use a foreach loop to assign the key-value pairs of each row to the corresponding properties of the new object. Adds a new object to the object array. Close the database connection.

Use Python's __contains__() function to define the containment operation of an object Use Python's __contains__() function to define the containment operation of an object Aug 22, 2023 pm 04:23 PM

Use Python's __contains__() function to define the containment operation of an object. Python is a concise and powerful programming language that provides many powerful features to handle various types of data. One of them is to implement the containment operation of objects by defining the __contains__() function. This article will introduce how to use the __contains__() function to define the containment operation of an object, and give some sample code. The __contains__() function is Pytho

What is the difference between arrays and objects in PHP? What is the difference between arrays and objects in PHP? Apr 29, 2024 pm 02:39 PM

In PHP, an array is an ordered sequence, and elements are accessed by index; an object is an entity with properties and methods, created through the new keyword. Array access is via index, object access is via properties/methods. Array values ​​are passed and object references are passed.

Use Python's __le__() function to define a less than or equal comparison of two objects Use Python's __le__() function to define a less than or equal comparison of two objects Aug 21, 2023 pm 09:29 PM

Title: Using Python's __le__() function to define a less than or equal comparison of two objects In Python, we can define comparison operations between objects by using special methods. One of them is the __le__() function, which is used to define less than or equal comparisons. The __le__() function is a magic method in Python and is a special function used to implement the "less than or equal" operation. When we compare two objects using the less than or equal operator (<=), Python

What is the Request object in PHP? What is the Request object in PHP? Feb 27, 2024 pm 09:06 PM

The Request object in PHP is an object used to handle HTTP requests sent by the client to the server. Through the Request object, we can obtain the client's request information, such as request method, request header information, request parameters, etc., so as to process and respond to the request. In PHP, you can use global variables such as $_REQUEST, $_GET, $_POST, etc. to obtain requested information, but these variables are not objects, but arrays. In order to process request information more flexibly and conveniently, you can

Detailed explanation of 5 loop traversal methods of Javascript objects Detailed explanation of 5 loop traversal methods of Javascript objects Aug 04, 2022 pm 05:28 PM

How to loop through Javascript objects? The following article will introduce five JS object traversal methods in detail, and briefly compare these five methods. I hope it will be helpful to you!

See all articles