php里public是什么,php - public,private以及protected有什么区别? - includeStdio

何时以及为什么要在课堂中使用public,private以及protected函数和变量?他们有什么区别?

例子:

// Public

public$variable;

public functiondoSomething() {

// ...

}

// Private

private$variable;

private functiondoSomething() {

// ...

}

// Protected

protected$variable;

protected functiondoSomething() {

// ...

}

你用:

public 范围使该变量/函数可以在任何地方,对象的其他类和实例中使用。

private 当你希望你的变量/函数只能在它自己的类中显示时。

protected 当你想让你的变量/函数在扩展包括父类的当前类的所有类中都可见时。

更多:(全面信息)

563fb719605c68a0b99c0d0e5caa751f.png

上市:

当你声明一个方法(函数)或属性(变量)时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.静态类成员对象的属性不被显示。

更多资源:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
#ifndef Node_hpp #define Node_hpp #include <stdio.h> class Node { public: virtual double Calc() const =0; virtual ~Node(){}; }; class NumberNode:public Node { public: double Calc() const; NumberNode(double number):number_(number){}; private: const double number_; }; class BinaryNode:public Node { public: BinaryNode(Node* left,Node* right):left_(left),right_(right){} ~BinaryNode(); protected: Node* const left_; Node* const right_; }; class UnaryNode:public Node { public: double Calc() const; UnaryNode(Node* child):child_(child){} ~UnaryNode(); protected: Node* const child_; }; class AddNode:public BinaryNode { public: AddNode(Node* left,Node* right):BinaryNode(left,right){} double Calc() const; }; class SubNode:public BinaryNode { public: SubNode(Node* left,Node* right):BinaryNode(left,right){} double Calc() const; }; class MultiplyNode:public BinaryNode { public: MultiplyNode(Node* left,Node* right):BinaryNode(left,right){} double Calc() const; }; class DivideNode:public BinaryNode { public: DivideNode(Node* left,Node* right):BinaryNode(left,right){} double Calc() const; }; class UMinusNode:public UnaryNode { public: UMinusNode(Node* child):UnaryNode(child){} double Calc() const; }; #endif#include "Node.hpp" #include <iostream> using namespace std; #include <cmath> double NumberNode:: Calc() const{ return number_; } BinaryNode::~BinaryNode(){ delete left_; delete right_; } UnaryNode::~UnaryNode(){ delete child_; } double AddNode:: Calc() const{ return left_->Calc()+right_->Calc(); } double SubNode:: Calc() const{ return left_->Calc()-right_->Calc(); } double MultiplyNode::Calc() const{ return left_->Calc()*right_->Calc(); } double DivideNode:: Calc() const{ double divisor=right_->Calc(); if (divisor!=0.0) { return left_->Calc()/divisor; } else { cout<<"Error:divisor by zreo"<<endl; return HUGE_VAL; } return left_->Calc()+right_->Calc(); } double UMinusNode::Calc() const{ return - child_->Calc(); }
06-02

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值