公开:
当您将方法(函数)或属性(变量)声明为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 成员来实现非凡 !
那么,反射是什么?
反射增加了对类、接口进行逆向工程的能力,
函数、方法和扩展。此外,它们还提供了方法
检索函数、类和方法的 doc cmets。
序言
我们有一个名为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 是 debugger functions。它们以人类可读的形式呈现有关变量的信息。这三个函数将显示 PHP 5 对象的 protected 和 private 属性。静态类成员将不显示。
更多资源: