php 5课程:公共,受保护和私有 [英] php 5 classes: public, protected and private

查看:52
本文介绍了php 5课程:公共,受保护和私有的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




最后给了php 5一个go,然后重新开始上课。

有人可以澄清公众,私人和受保护给我?


我引用php手册:"属性或方法的可见性可以是

,通过在声明前添加关键字来定义:public ,

受保护或私有。可以访问公开声明的项目

无处不在。


但我应该阅读......可以在给定的类中随处访问。

或......可以被所有其他类访问。​​ ?


工作

解决方案

jopperdepopper写道:





最后给了php 5一个go,然后重新开始上课。

有人可以澄清公共,私人和受我保护?


我引用php手册:"属性或方法的可见性可以是

通过在声明前添加前缀使用关键字:public,

protected或private。可以访问公开声明的项目

无处不在。


但我应该阅读......可以在给定的类中随处访问。

或......可以被所有其他类访问。​​ ?


工作



工作,


你应该读can $ b / b
私人会员只能由班级成员访问。

受保护的成员可以由班级成员或派生成员访问

class。

任何人都可以访问公共成员,包括其他课程,

函数和任何其他代码。


-

==================

删除x来自我的电子邮件地址

Jerry Stuckle

JDS计算机培训公司
js ******* @ attglobal.net

==================



>

您应该阅读可随处访问。


私人会员只能由班级成员访问。

受保护的会员可以由班级成员或派生的

班级访问。

任何人都可以访问公共成员,包括其他类,

函数和任何其他代码。



谢谢Jerry。我正在尝试对课程有点了解php 5的方法

,到目前为止还很难。我没有看到''公共,保护和私人'背后的'为什么''

'和抽象,

接口和诸如此类的东西。感觉事情变得过于复杂

不知怎的......或者只是我对此缺乏经验...


任何其他阅读材料在这个建议,有人?


jopperdepopper写道:


>>您应该阅读可以随处访问。

私人会员只能由班级成员访问。
受保护的成员可以被成员访问类或派生的类。
公共成员可以被任何人访问,包括其他类,
函数和任何其他代码。




谢谢Jerry。我正在尝试对课程有点了解php 5的方法

,到目前为止还很难。我没有看到''公共,保护和私人'背后的'为什么''

'和抽象,

接口和诸如此类的东西。感觉事情变得过于复杂

不知怎的......或者只是我对此缺乏经验...


任何其他阅读材料在这个建议,有人?



寻找一些关于OO理论和设计的好书。


其中两个概念OO是''封装''和''方法''。


封装意味着对象的内部只由那个

对象管理而不是其他任何人都可以使用在PHP中,这些是私有的

成员。


方法在PHP中实现为函数。他们对这个对象进行操作。


这个想法是将类的实现与

完全分开使用它。这意味着您可以以任何方式更改课程。因为公共接口保持不变,所以没有区别。


一个很好的例子就是浮点数。它们存储为


x * 2 ^ y


X是尾数,y是基数2指数。例如,8将是

存储为x = 1且y = 3,因为8是1 * 2 ^ 3。但所有这一切都隐藏在幕后;
幕后;你可以对浮点数进行操作,例如加,减,打印等




如果在其他时候PHP将实现更改为什么东西

否则,你的代码都不会改变。


-

========== ========

删除x来自我的电子邮件地址

Jerry Stuckle

JDS计算机培训公司
js ******* @ attglobal.net

==================


Hi,

finally giving php 5 a go, and going over the new approach to classes.
Can someone clarify the public, private and protected to me?

I quote the php manual: "The visibility of a property or method can be
defined by prefixing the declaration with the keywords: public,
protected or private. Public declared items can be accessed
everywhere."

But should I read "...can be accessed everywhere within a given class."
or "...can be accessed by all other classes." ?

Job

解决方案

jopperdepopper wrote:

Hi,

finally giving php 5 a go, and going over the new approach to classes.
Can someone clarify the public, private and protected to me?

I quote the php manual: "The visibility of a property or method can be
defined by prefixing the declaration with the keywords: public,
protected or private. Public declared items can be accessed
everywhere."

But should I read "...can be accessed everywhere within a given class."
or "...can be accessed by all other classes." ?

Job

Job,

You should read "can be accessed everywhere".

Private members can be accessed by members of the class only.
Protected members can be accessed by members of the class or a derived
class.
Public members can be accessed by anyone, including other classes,
functions and any other code.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================


>
You should read "can be accessed everywhere".

Private members can be accessed by members of the class only.
Protected members can be accessed by members of the class or a derived
class.
Public members can be accessed by anyone, including other classes,
functions and any other code.


Thanks Jerry. I''m trying to make a bit of sense of the php 5 approach
to classes, and so far having a hard time. I fail to see the ''why''
behind the ''public, protected and private'' and stuff like abstraction,
interfaces and whatnot. Feels like things are being over-complicated
somehow... or it''s just my being inexperienced on this...

Any other reading material on this suggested, someone?


jopperdepopper wrote:

>>You should read "can be accessed everywhere".

Private members can be accessed by members of the class only.
Protected members can be accessed by members of the class or a derived
class.
Public members can be accessed by anyone, including other classes,
functions and any other code.


Thanks Jerry. I''m trying to make a bit of sense of the php 5 approach
to classes, and so far having a hard time. I fail to see the ''why''
behind the ''public, protected and private'' and stuff like abstraction,
interfaces and whatnot. Feels like things are being over-complicated
somehow... or it''s just my being inexperienced on this...

Any other reading material on this suggested, someone?

Look for some good books on OO theory and design.

Two of the concepts in OO are ''encapsulation'' and ''methods''.

Encapsulation means the internals of an object are managed only by that
object and are not available to anyone else. In PHP these are private
members.

Methods are implemented as functions in PHP. They operate on the object.

The idea is to completely separate the implementation of the class from
the use of it. It means you can change the class any way you want. As
long as the public interface remains the same, it makes no difference.

A good example of this is floating point numbers. They are stored as a

x * 2^y

X is the mantissa, y is the base 2 exponent. For instance, 8 would be
stored as x=1 and y=3, because 8 is 1 * 2^3. But all of this is hidden
behind the scenes; you can do operations on a floating point number such
as add, subtract, print, etc.

And if at some other time PHP changed the implementation to something
else, none of your code would change.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================


这篇关于php 5课程:公共,受保护和私有的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆