上市:
当您将方法(函数)或属性(变量)声明为时public
,可以通过以下方式访问这些方法和属性:
- 声明它的同一个类。
- 继承上述声明的类的类。
- 此类之外的任何外来元素也可以访问这些东西。
例:
<?php
class GrandPa
{
public $name='Mark Henry'; // A public variable
}
class Daddy extends GrandPa // Inherited class
{
function displayGrandPaName()
{
return $this->name; // The public variable will be available to the inherited class
}
}
// Inherited class Daddy wants to know Grandpas Name
$daddy = new Daddy;
echo $daddy->displayGrandPaName(); // Prints 'Mark Henry'
// Public variables can also be accessed outside of the class!
$outsiderWantstoKnowGrandpasName = new GrandPa;
echo $outsiderWantstoKnowGrandpasName->name; // Prints 'Mark Henry'
受保护的:
当将方法(函数)或属性(变量)声明为时protected
,可以通过以下方式访问这些方法和属性
外部成员无法访问这些变量。从某种意义上说,“局外人”不是声明的类本身的对象实例。
例:
<?php
class GrandPa
{
protected $name = 'Mark Henry';
}
class Daddy extends GrandPa
{
function displayGrandPaName()
{
return $this->name;
}
}
$daddy = new Daddy;
echo $daddy->displayGrandPaName(); // Prints 'Mark Henry'
$outsiderWantstoKnowGrandpasName = new GrandPa;
echo $outsiderWantstoKnowGrandpasName->name; // Results in a Fatal Error
确切的错误是这样的:
PHP致命错误:无法访问受保护的属性GrandPa :: $ name
私人的:
当您将方法(函数)或属性(变量)声明为时private
,可以通过以下方式访问这些方法和属性:
外部成员无法访问这些变量。从某种意义上来说,局外人不是声明的类本身的对象实例,甚至不是继承声明的类的类的对象实例。
例:
<?php
class GrandPa
{
private $name = 'Mark Henry';
}
class Daddy extends GrandPa
{
function displayGrandPaName()
{
return $this->name;
}
}
$daddy = new Daddy;
echo $daddy->displayGrandPaName(); // Results in a Notice
$outsiderWantstoKnowGrandpasName = new GrandPa;
echo $outsiderWantstoKnowGrandpasName->name; // Results in a Fatal Error
确切的错误消息将是:
注意:未定义的属性:Daddy :: $ name
致命错误:无法访问私有属性GrandPa :: $ name
用反射剖析爷爷班
这个主题并没有真正超出范围,我在这里添加它只是为了证明反射功能确实强大。正如我在上述三个实施例所述,protected
和private
构件(属性和方法)不能被访问之类的外部。
然而,随着反射,你可以做的超乎寻常的甚至访问protected
和private
类的成员外!
好吧,反射是什么?
反射增加了对类,接口,函数,方法和扩展进行反向工程的能力。此外,它们提供了检索函数,类和方法的文档注释的方法。
前言
我们有一个名为的类,Grandpas
并说我们有三个属性。为了便于理解,请考虑以下三个具有名称的爷爷:
让我们让他们(分配调节剂)public
,protected
并private
分别。您非常了解,protected
并且private
无法在课堂之外访问成员。现在,让我们使用反射与该语句矛盾。
编码
<?php
class GrandPas // The Grandfather's class
{
public $name1 = 'Mark Henry'; // This grandpa is mapped to a public modifier
protected $name2 = 'John Clash'; // This grandpa is mapped to a protected modifier
private $name3 = 'Will Jones'; // This grandpa is mapped to a private modifier
}
# Scenario 1: without reflection
$granpaWithoutReflection = new GrandPas;
# Normal looping to print all the members of this class
echo "#Scenario 1: Without reflection<br>";
echo "Printing members the usual way.. (without reflection)<br>";
foreach($granpaWithoutReflection as $k=>$v)
{
echo "The name of grandpa is $v and he resides in the variable $k<br>";
}
echo "<br>";
#Scenario 2: Using reflection
$granpa = new ReflectionClass('GrandPas'); // Pass the Grandpas class as the input for the Reflection class
$granpaNames=$granpa->getDefaultProperties(); // Gets all the properties of the Grandpas class (Even though it is a protected or private)
echo "#Scenario 2: With reflection<br>";
echo "Printing members the 'reflect' way..<br>";
foreach($granpaNames as $k=>$v)
{
echo "The name of grandpa is $v and he resides in the variable $k<br>";
}
输出:
#Scenario 1: Without reflection
Printing members the usual way.. (Without reflection)
The name of grandpa is Mark Henry and he resides in the variable name1
#Scenario 2: With reflection
Printing members the 'reflect' way..
The name of grandpa is Mark Henry and he resides in the variable name1
The name of grandpa is John Clash and he resides in the variable name2
The name of grandpa is Will Jones and he resides in the variable name3
常见的误解:
请不要与以下示例混淆。如您所见,如果不使用反射,则不能在类外部访问private
和protected
成员
<?php
class GrandPas // The Grandfather's class
{
public $name1 = 'Mark Henry'; // This grandpa is mapped to a public modifier
protected $name2 = 'John Clash'; // This grandpa is mapped to a protected modifier
private $name3 = 'Will Jones'; // This grandpa is mapped to a private modifier
}
$granpaWithoutReflections = new GrandPas;
print_r($granpaWithoutReflections);
输出:
GrandPas Object
(
[name1] => Mark Henry
[name2:protected] => John Clash
[name3:GrandPas:private] => Will Jones
)
调试功能
print_r
,var_export
和var_dump
是调试功能。它们以人类可读的形式显示有关变量的信息。这三个函数将揭示PHP 5对象的protected
和private
属性。静态类成员将不会显示。
更多资源: