search
Homephp教程php手册PHP 作用域解析运算符(::)

PHP 作用域解析运算符(::)

Jun 13, 2016 pm 12:14 PM
joomlaphpScopeSource codeoflookparseoperator

Scope Resolution Operator (::)
今天 看joomla源码的时候,才意识到。原来这个操作符还可以访问类的非静态方法啊。真的让我吃惊不好。一直以为作用域解析运算符只能访问类的static方法和static成员变量。
如果各位不相信,下面有个简单的小测试代码可以证明这个。

复制代码 代码如下:


class A{
private $_name = 'A';
function __construct(){
echo 'A construct
';
}
function test(){
echo 'A test()
';
}
}
class B extends A{
private $_name = 'B';
function __construct(){
parent::__construct();
echo 'B construct
';
}
function test(){
echo 'B test()';
}
}
A::test();
echo '#########
';
B::test();


这段代码输入的结果为:

复制代码 代码如下:


A test()
#########
B test()


虽然A类中的test()和B类中的test都不是 static方法,但是一样可以用 “类名::方法名称(参数列表)” 的样式进行正确调用。他的效果和 new 一个类的实例,然后用这个实例调用
test方法是一个样的。
但是,如果我需要在test方法中打印name属性,直接用::来调用 会是怎么个情况那.我们首先来修改下 上面的代码。

复制代码 代码如下:


class A{
private $_name = 'A';
function __construct(){
echo 'A construct
';
}
function test(){
echo 'A test()
', $this->$_name,'
';
}
}
class B extends A{
private $_name = 'B';
function __construct(){
parent::__construct();
echo 'B construct
';
}
function test(){
echo 'B test()', $this->_name,'
';
}
}
A::test();
echo '#########
';
B::test();


上面的代码运行的结果 如下:

复制代码 代码如下:


Fatal error: Using $this when not in object context in D:\www\test\scoperefe.php on line 9
[html]
那有的朋友就说了。你压根就没有实例化类A,当然不能直接用$this->_name的方式来访问成员变量$_name了,那么,是不是修改成self::$_name就行了哪?
说干就干,下面把上面的代码修改下
[code]
class A{
private $_name = 'A';
function __construct(){
echo 'A construct
';
}
function test(){
echo 'A test()
', self::$_name,'
';
}
}
class B extends A{
private $_name = 'B';
function __construct(){
parent::__construct();
echo 'B construct
';
}
function test(){
echo 'B test()', $this->_name,'
';
}
}
A::test();
echo '#########
';
B::test();


再运行上面的代码,结果如下:

复制代码 代码如下:


A test() Fatal error: Access to undeclared static property: A::$_name in D:\www\test\scoperefe.php on line 9



哦,原来不能用self 关键字访问当前类的非static方法。
现在,如果想正确的调用这个方法,有2个做法:
1、首先实例化类,然后用对象调用就可以直接使用$this->_name进行调用了;
2、将成员变量$_name设置为static;

上面的问题,相信大家都能够正确的处理。

其实我真正想说的是:
如果一个方法可以不进行实例化就调用,那么我们最好把这个方法使用static关键字修饰下。在实现方法的时候,只调用该类的static成员变量。这样就不会出现上面遇到问题了。
如果一个方法没有设置为static的方法。那么,最安全的做法还是用实例对象进行调用更为安全,因为,说不定什么时候就需要修改该方法的实现,在修改的时候,说不定就要调用该类中的
非static成员变量(因为,很大程度上在修改方法的实现的时候,已经忘记还有用类名直接调用这么一说)。

个人愚见。
Statement
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

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software