Home php教程 php手册 PHP的基本常识(2)

PHP的基本常识(2)

Jun 13, 2016 am 10:49 AM
error NOT object oop php this using Basic object common sense of

对象 OOP
1.Fatal error: Using $this when not in object context
这个错误刚学 OOP 肯定容易出现,因为有个概念你没有真正理解。 类的可访问性(accessible),也可以说是作用域, 你还可以认为是 1个 中国人 在国外,他不属于哪个文化,他不讲外语(可能他知道点);但是他无法通过自己跟老外沟通,因为他们不是在一个共同国度出生。
那么错误是如何发生的呢?看下面的例子:
 
 1  2 class Trones{
 3     static public $fire = "I am fire.";
 4      public $water = "I am water";
 5    
 6     static function getFire( ) {
 7         return $this->fire ; // wrong
 8     }
 9     static function getWater( ) {
10         return $self::water ;  // wrong
11     }
12    
13     static function Fire( ) {
14         return self::$fire ;  // be sure you use self to access the static property before you invoke the function
15     }
16 }
17
18 /*
19 Fatal error: Using $this when not in object context
20 */
21 //echo Trones::getFire( ) ;
22 //echo Trones::getWater( ) ;
23
24 // correct
25 echo Trones::Fire( );
26 echo "
" ;
27 $trones = new Trones ;
28 $trones->fire ; // Notice: Undefined property: Trones::$fire (base on defferent error setting) simple is error
29 echo Trones::$fire ;
 
这个错误很经典, 也很实用,先看 static 的定义:
Declaring class properties or methods as static makes them accessible without needing an instantiation of the class. A property declared as static can not be accessed with an instantiated class object (though a static method can).
翻译:定义一个类的属性或方法为静态时,可以使他们在不需要初始化一个类时就能直接访问 。一个被定义为了静态的属性不能被类的对象用对象操作符访问* -> *,(可以通过静态的方法访问)。
例子说明:
7行 10行 犯了同一个错误,第一个是用对象操作符来访问静态变量。你看看定义,$this 是一个伪变量 相当于 object,一个实例。你用对象操作符 -> 访问就会报错。
同样你也不能用 静态操作符 :: 来访问一个公共变量 。 正确的访问应该是 14行 25行,一个是在类的定义里访问(self:: === Trones::),一个是在类的外部访问。
对于继承类,以上的规则同样适合。
------------------------------------------------------ 分割线--------------------------------------------------------------------------------------
2.Fatal error: Call to private method
最近有部连续剧很好看,叫权利的游戏,我们假设有 3方人马, 7个国王, 平民, 龙女。 他们三方人马在下面争夺最终的胜利, 也就是王冠。
下面的故事还有一个标题:类的可见性(visibility) 你如果知道最终的答案,解释部分你可以略过了。
 
 
 1  2
 3 class Trones {
 4     protected $fire = " fire ";
 5     public $water = " water " ;
 6     static private $trones  = "Trones";
 7    
 8     protected function getFire( ) {
 9         $this->fire ;
10     }
11    
12     static public function TheDragenOfMather( ) {
13         return __METHOD__." use ".$this->getFire()." gets the ".self::getTrones( ) ;
14     }
15    
16     static public function getWater( ) {
17         return __METHOD__ ;
18     }
19    
20     static private function getTrones( ) {
21         return  self::$trones ;
22     }
23
24 }
25
26 class Kings extends Trones {
27     static function TheSevenKing( ) {
28         return __METHOD__."gets the ".self::getTrones( );
29     }
30 }
31
32 class People extends Trones{
33     static function ThePeople( ) {
34         return __METHOD__."gets the ".self::getTrones( );
35     }
36 }
37 echo Kings::TheSevenKing( ) ;
38 echo Trones::TheDragenOfMather( ) ;
39 echo People::ThePeople( ) ;
 
 
 
正确答案是:7国征战 内斗,平民死伤无数,龙女想乘机渔翁得利;可惜 最终谁也没有得到皇冠和胜利。哈哈。
当static 碰到 private ,结合产生复杂,也产生美;就像抽象的人,像我们大学老师讲的数学课;(不过网易的公开数学课很好)
如果想要龙女 获得最后的胜利, 你只要帮她一把 将13行的 $this->getFire() 这部分去掉就可以了。同样的道理 你无法在一个静态函数里 使用任何对象操作符。
怎么使人民获得王冠呢? 你去奋斗吧!
 
如果你不构建大型的框架和网站 这些概念比如 Interface Implement abstract 。。。 你还是不知道的好。

 

 

摘自 warcraft

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

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)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

7 PHP Functions I Regret I Didn't Know Before 7 PHP Functions I Regret I Didn't Know Before Nov 13, 2024 am 09:42 AM

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? Apr 03, 2025 am 12:03 AM

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

See all articles