


The difference between public, protected and private, protectedprivate_PHP tutorial
The difference between public, protected and private, protectedprivate
Draw the conclusion through className::functionNmae(), instantiation, parent, self
className::functionNmae() needs to be a static method (public mode)
parent::functionNmae() The parent class must be a static method (public and protected modes)
self::functionNmae() This class must be a static method (three modes)
Instantiation: After instantiation, you can access static methods or ordinary methods in public mode
1. Direct access (through className::functionNmae() and instantiation):
Conclusion:
Nmae() access
protected
| Not accessible through instance $obj->functionName() | Not accessible through className::functionNmae()
private | Not accessible through className::functionNmae() Example$obj-> ;functionName() access
2. Direct access (self::functionNmae())
Conclusion:
self::functionNmae(); needs to be a static method (all three modes are available)
<?<span>php </span><span>/*</span><span>* * * @authors HG (hg0728@qq.com) * @date 2015-05-26 17:12:02 * @version 1.0 </span><span>*/</span> <span>header</span>("Content-type:text/html;charset=utf-8"<span>); </span><span>class</span><span> A { </span><span>static</span> <span>$name</span>='这是类A的静态变量'<span>; </span><span>static</span> <span>public</span> <span>function</span><span> stc_pc(){ </span><span>echo</span> '这是类A的公共静态方法<br />'<span>; } </span><span>static</span> <span>function</span><span> stc(){ </span><span>echo</span> '这是类A的静态方法<br />'<span>; } </span><span>static</span> <span>protected</span> <span>function</span><span> stc_pd(){ </span><span>echo</span> '这是类A受保护的静态方法<br />'<span>; } </span><span>static</span> <span>private</span> <span>function</span><span> stc_pe(){ </span><span>echo</span> '这是类A私有的静态方法<br />'<span>; } } A</span>::stc_pc();<span>//</span><span>可访问</span> <span> A</span>::stc();<span>//</span><span>可访问</span> <span> A</span>::<span>$name</span>;<span>//</span><span>不报错 //A::stc_pd();//不可访问 //A::stc_pe();//不可访问</span> <span>echo</span> '<br><br>通过实例化访问<br><br>'<span>; </span><span>$a</span> = <span>new</span><span> A(); </span><span>$a</span>->stc_pc();<span>//</span><span>可访问</span> <span>$a</span>->stc();<span>//</span><span>可访问 //$a->$name;//报错 //$a->stc_pd();//不可访问 //$a->stc_pe();//不可访问</span> <span>/*</span><span>******************* | 可以 通过 className::functionNmae() 访问 public | | 可以 通过实例 $obj->functionName() 访问 | 不可 通过 className::functionNmae() 访问 protected | | 不可 通过实例 $obj->functionName() 访问 | 不可 通过 className::functionNmae() 访问 private | | 不可 通过实例 $obj->functionName() 访问 *******************</span><span>*/</span>
3. Access parent class through subclass (parent::functionNmae() and instantiation)
Conclusion:
<?<span>php </span><span>/*</span><span>* * * @authors HG (hg0728@qq.com) * @date 2015-05-26 17:18:50 * @version 1.0 </span><span>*/</span> <span>header</span>("Content-type:text/html;charset=utf-8"<span>); </span><span>class</span><span> C { </span><span>static</span> <span>$name</span> = "静态变量"<span>; </span><span>static</span> <span>public</span> <span>function</span><span> stc_pc(){ </span><span>echo</span> '静态公共方法<br>'<span>; self</span>::<span>stc_pd(); self</span>::<span>stc_pe(); self</span>::<span>stc(); </span><span>//</span><span>self::func();//报错但有结果</span> <span> } </span><span>static</span> <span>protected</span> <span>function</span><span> stc_pd(){ </span><span>echo</span> '静态受保护方法<br>'<span>; } </span><span>static</span> <span>private</span> <span>function</span><span> stc_pe(){ </span><span>echo</span> '静态受保护方法<br>'<span>; } </span><span>static</span> <span>function</span><span> stc(){ </span><span>echo</span> '静态普通方法<br>'<span>; } </span><span>function</span><span> func(){ </span><span>echo</span> '普通方法<br>'<span>; } } C</span>::<span>stc_pc(); </span><span>/*</span><span>***** self::functionNmae();需是静态方法(三种模式都可) *****</span><span>*/</span>
$obj->functionNmae(); private is not accessible
<?<span>php </span><span>/*</span><span>* * * @authors HG (hg0728@qq.com) * @date 2015-05-26 17:18:50 * @version 1.0 </span><span>*/</span> <span>header</span>("Content-type:text/html;charset=utf-8"<span>); </span><span>include</span> '/class_a.php'<span>; </span><span>class</span> B <span>extends</span><span> A{ </span><span>static</span> <span>public</span> <span>function</span><span> index(){ parent</span>::<span>stc_pc(); parent</span>::<span>stc(); parent</span>::<span>stc_pd(); parent</span>::<span>$name</span>;<span>//</span><span>不报错 //parent::stc_pe();//不可访问</span> <span>echo</span> '<br><br>通过实例化访问<br><br>'<span>; </span><span>$a</span> = <span>new</span><span> A(); </span><span>$a</span>-><span>stc_pc(); </span><span>$a</span>-><span>stc(); </span><span>$a</span>-><span>stc_pd(); </span><span>$a</span>-><span>$name</span>;<span>//</span><span>报错 //$a->stc_pe();//不可</span> <span> } } B</span>::<span>index(); </span><span>/*</span><span>***** 在子类中通过: parent::functionNmae(); private不可访问 $obj->functionNmae(); private不可访问 *****</span><span>*/</span>
www.bkjia.com
true
Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Alipay PHP...

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,

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

Article discusses essential security features in frameworks to protect against vulnerabilities, including input validation, authentication, and regular updates.

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...

The article discusses adding custom functionality to frameworks, focusing on understanding architecture, identifying extension points, and best practices for integration and debugging.

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

Session hijacking can be achieved through the following steps: 1. Obtain the session ID, 2. Use the session ID, 3. Keep the session active. The methods to prevent session hijacking in PHP include: 1. Use the session_regenerate_id() function to regenerate the session ID, 2. Store session data through the database, 3. Ensure that all session data is transmitted through HTTPS.
