何时以及为什么要在课堂中使用public,private以及protected函数和变量?他们有什么区别?
例子:
// Public
public$variable;
public functiondoSomething() {
// ...
}
// Private
private$variable;
private functiondoSomething() {
// ...
}
// Protected
protected$variable;
protected functiondoSomething() {
// ...
}
你用:
public 范围使该变量/函数可以在任何地方,对象的其他类和实例中使用。
private 当你希望你的变量/函数只能在它自己的类中显示时。
protected 当你想让你的变量/函数在扩展包括父类的当前类的所有类中都可见时。
更多:(全面信息)
上市:
当你声明一个方法(函数)或属性(变量)时public,可以通过以下方式访问这些方法和属性:
宣布它的同一个类。
继承上面声明的类的类。
此课程以外的任何外国人也可以访问这些内容。
例:
{
public$name='Mark Henry'; // A public variable
}
class Daddy extends GrandPa // Inherited class
{
functiondisplayGrandPaName()
{
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,可以通过访问这些方法和属性
宣布它的同一个类。
继承上面声明的类的类。
局外人成员不能访问这些变量。“局外人”是指它们不是被声明的类本身的对象实例。
例:
{
protected$name= 'Mark Henry';
}
class Daddy extends GrandPa
{
functiondisplayGrandPaName()
{
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,可以通过以下方式访问这些方法和属性:
宣布它的同一个类。
局外人成员不能访问这些变量。局外人的意义在于,它们不是被声明的类本身的对象实例,甚至是继承声明的类的类。
例:
{
private$name= 'Mark Henry';
}
class Daddy extends GrandPa
{
functiondisplayGrandPaName()
{
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构件(属性和方法)不能在类的外部访问。
但是,通过反思,您甚至可以通过访问和课堂以外的成员来做到超乎寻常的事情!protectedprivate
那么,反思是什么?
反思增加了对类,接口,函数,方法和扩展进行反向工程的能力。另外,它们提供了获取文档注释以获取函数,类和方法的方法。
前言
我们有一个名为,Grandpas并说我们有三个属性。为了容易理解,考虑有三个名字为grandpa的:
马克亨利
约翰克莱什
威尔琼斯
让我们让他们(分配调节剂)public,protected并private分别。你很清楚,protected和private成员不能在类外访问。现在让我们用反思来反驳这个陈述。
代码
{
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 classecho"#Scenario 1: Without reflection
";echo"Printing members the usual way.. (without reflection)
";
foreach($granpaWithoutReflectionas$k=>$v)
{echo"The name of grandpa is $v and he resides in the variable $k
";
}echo"
";
#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
";echo"Printing members the 'reflect' way..
";
foreach($granpaNamesas$k=>$v)
{echo"The name of grandpa is $v and he resides in the variable $k
";
}
输出:
#Scenario 1: Without reflection
Printingmembers the usual way.. (Withoutreflection)
Thename of grandpais Mark Henry andhe residesinthe variable name1#Scenario 2: With reflection
Printingmembers the'reflect'way..
Thename of grandpais Mark Henry andhe residesinthe variable name1Thename of grandpais John Clash andhe residesinthe variable name2Thename of grandpais Will Jones andhe residesinthe variable name3
常见的误解:
请不要混淆下面的例子。正如您仍然可以看到的那样,在不使用反射的情况下,无法在课程外部访问private和protected成员
{
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是调试器功能。他们以可读的形式呈现有关变量的信息。这三个函数将揭示protected和private将与PHP 5.静态类成员对象的属性不被显示。
更多资源: