In my abstract class My_Class
, I have a method My_Class::foo()
that's called from within another method belonging to the class. For example:
在我的抽象类My_Class中,我有一个方法My_Class :: foo(),它是从属于该类的另一个方法中调用的。例如:
abstract class My_Class {
function foo() {
// Stuff.
}
function bar() {
$this->foo();
}
// More methods etc...
}
Now, I'm in the process of extending my abstract class. For example:
现在,我正在扩展我的抽象类。例如:
class My_Class_Extended extends My_Class {
}
As you'll know, both My_Class::foo()
and My_Class::bar()
are inherited by My_Class_Extended
.
如您所知,My_Class :: foo()和My_Class :: bar()都由My_Class_Extended继承。
I never want My_Class::foo()
to be called outside of My_Class
or My_Class_Extended
so I know not to make its visibility public
. My problem is, I'm unsure whether to make its visibility protected
or private
.
我从不希望在My_Class或My_Class_Extended之外调用My_Class :: foo(),所以我知道不要公开它的可见性。我的问题是,我不确定是否保护其可见性或保密。
My question
Considering how I'm calling My_Class::foo()
in the above scenario, should it be made protected
or private
? I'm not sure if the call to My_Class::foo()
is coming from the child or parent class.
考虑到我在上面的场景中如何调用My_Class :: foo(),它应该被保护还是私有?我不确定对My_Class :: foo()的调用是来自子类还是父类。
Thanks in advance.
提前致谢。
1 个解决方案
#1
0
One-line: protected, because you want your child classes have access
单行:受保护,因为您希望您的子类具有访问权限
The visibility modifiers work like this:
可见性修饰符的工作方式如下:
- public: visible from everywhere.
-
-
$c = new My_Class_Extended(); $c->thisIsPublic();
. This doesn't work with private or protected
$ c = new My_Class_Extended(); $ C-> thisIsPublic();.这不适用于私人或受保护
-
- protected: visible inside of the class and it's child classes
-
- You can only call these functions inside of a function belonging to a class that derives from your parent class, or the parent class itself.
您只能在属于从父类或父类本身派生的类的函数内调用这些函数。
- private: Only visible for the class it's defined in. You cannot call private functions in child classes, nor outside of the class.
公众:从各处可见。
$ c = new My_Class_Extended(); $ C-> thisIsPublic();.这不适用于私人或受保护
protected:在类内部可见,它是子类
您只能在属于从父类或父类本身派生的类的函数内调用这些函数。
private:仅对其定义的类可见。您不能在子类中调用私有函数,也不能在类之外调用私函数。
The modifiers are ordered inclusively. So a protected is more restrictive than public, and private is more restrictive than protected.
修饰语包含在内。因此受保护比公共更具限制性,而私有受限制比受保护更严格。
You want protected, because you want to call them inside a child class, but not outside of it.
你想要保护,因为你想在子类中调用它们,但不是在它之外。
By the way: In your context, the abstract keyword only ensures that you cannot create an instance from My_Class
.
顺便说一句:在您的上下文中,abstract关键字仅确保您无法从My_Class创建实例。
#1
0
One-line: protected, because you want your child classes have access
单行:受保护,因为您希望您的子类具有访问权限
The visibility modifiers work like this:
可见性修饰符的工作方式如下:
- public: visible from everywhere.
-
-
$c = new My_Class_Extended(); $c->thisIsPublic();
. This doesn't work with private or protected
$ c = new My_Class_Extended(); $ C-> thisIsPublic();.这不适用于私人或受保护
-
- protected: visible inside of the class and it's child classes
-
- You can only call these functions inside of a function belonging to a class that derives from your parent class, or the parent class itself.
您只能在属于从父类或父类本身派生的类的函数内调用这些函数。
- private: Only visible for the class it's defined in. You cannot call private functions in child classes, nor outside of the class.
公众:从各处可见。
$ c = new My_Class_Extended(); $ C-> thisIsPublic();.这不适用于私人或受保护
protected:在类内部可见,它是子类
您只能在属于从父类或父类本身派生的类的函数内调用这些函数。
private:仅对其定义的类可见。您不能在子类中调用私有函数,也不能在类之外调用私函数。
The modifiers are ordered inclusively. So a protected is more restrictive than public, and private is more restrictive than protected.
修饰语包含在内。因此受保护比公共更具限制性,而私有受限制比受保护更严格。
You want protected, because you want to call them inside a child class, but not outside of it.
你想要保护,因为你想在子类中调用它们,但不是在它之外。
By the way: In your context, the abstract keyword only ensures that you cannot create an instance from My_Class
.
顺便说一句:在您的上下文中,abstract关键字仅确保您无法从My_Class创建实例。