PHP: 调用父方法的中断方法 class
PHP: Interhited method calling a method in the parent class
class Person {
function foo() {
// Code.
}
function bar() {
$this->foo();
}
}
class Pete extends Person {
}
在上例中,当从 Person::bar()
中调用 Person::foo()
时,调用发生在 Person
class 中。因此,我 可以 提高 Person::foo()
private
.
的可见性
如您所知,当我扩展 Person
class 时,Person::foo()
和 Person::bar()
由子 class Pete
。因此,继承方法 Pete::bar()
调用 Pete::foo()
。
我的问题
在 Pete::bar()
中,对 Pete::foo()
的调用被认为是来自 a) 父 class 或 b) 子 class?
From within Pete::bar(), is the call to Pete::foo() considered to come from a) the parent class or b) the child class?
都没有。从技术上讲,foo()
不存在于 class Pete
中。它存在于 Person
并被 Pete
继承。无论如何,谁在调用该方法基于调用。
例如:
$person = new Person();
$person->foo(); // invocation by `Person` object
$pete = new Pete();
$pete->foo(); // invocation by `Pete` object
I'm trying to determine method visibility
如果要继承这些方法,仅限public
或protected
。 private
方法不被继承。我鼓励您阅读更多关于 visibility.
的内容
如 中所述,您可以创建方法 private
并通过另一个 public
方法访问它。
如果一个方法具有 private
的可见性,那么显式调用它的所有代码必须驻留在相同的 class 定义中。这几乎就是规则,仅此而已。通过继承的间接调用工作得很好。
class Foo {
private function bar() { }
public function baz() { /* here be dragons */ }
}
class Child extends Foo { }
在Child
class中你可能不会写$this->bar()
。它会失败。 bar
方法是 private
到 class Foo
,没有其他代码可以调用它。您 可以 调用 baz
尽管随时随地,它是 public
并且可以从其他代码调用。 无论 baz
在内部做什么都是您关心的 none。 如果 baz
在内部调用 bar
,那很好。它的代码驻留在 Foo
中,因此可以调用 bar
.
如果将其设为私有,Person::foo
只能从 Person
调用
class Person {
//Can only be called inside of Person
private function foo() {
// Code.
}
public function bar() {
//can be called
$this->foo();
}
}
您仍然可以调用 bar
,因为它是 public。 bar
仍然可以访问 foo
,因为它是来自 Person
.
的方法
您不能从 Pete
调用 foo
,因为它仅在 Person 中可见。
class Pete extends Person {
public function doSomething() {
//Works because bar itself calls foo
$this->bar();
}
}
如果您想直接从 Pete
调用 foo
或覆盖它,您需要声明它 protected
而不是 private
.
class Person {
function foo() {
// Code.
}
function bar() {
$this->foo();
}
}
class Pete extends Person {
}
在上例中,当从 Person::bar()
中调用 Person::foo()
时,调用发生在 Person
class 中。因此,我 可以 提高 Person::foo()
private
.
如您所知,当我扩展 Person
class 时,Person::foo()
和 Person::bar()
由子 class Pete
。因此,继承方法 Pete::bar()
调用 Pete::foo()
。
我的问题
在 Pete::bar()
中,对 Pete::foo()
的调用被认为是来自 a) 父 class 或 b) 子 class?
From within Pete::bar(), is the call to Pete::foo() considered to come from a) the parent class or b) the child class?
都没有。从技术上讲,foo()
不存在于 class Pete
中。它存在于 Person
并被 Pete
继承。无论如何,谁在调用该方法基于调用。
例如:
$person = new Person();
$person->foo(); // invocation by `Person` object
$pete = new Pete();
$pete->foo(); // invocation by `Pete` object
I'm trying to determine method visibility
如果要继承这些方法,仅限public
或protected
。 private
方法不被继承。我鼓励您阅读更多关于 visibility.
如 private
并通过另一个 public
方法访问它。
如果一个方法具有 private
的可见性,那么显式调用它的所有代码必须驻留在相同的 class 定义中。这几乎就是规则,仅此而已。通过继承的间接调用工作得很好。
class Foo {
private function bar() { }
public function baz() { /* here be dragons */ }
}
class Child extends Foo { }
在Child
class中你可能不会写$this->bar()
。它会失败。 bar
方法是 private
到 class Foo
,没有其他代码可以调用它。您 可以 调用 baz
尽管随时随地,它是 public
并且可以从其他代码调用。 无论 baz
在内部做什么都是您关心的 none。 如果 baz
在内部调用 bar
,那很好。它的代码驻留在 Foo
中,因此可以调用 bar
.
如果将其设为私有,Person::foo
只能从 Person
class Person {
//Can only be called inside of Person
private function foo() {
// Code.
}
public function bar() {
//can be called
$this->foo();
}
}
您仍然可以调用 bar
,因为它是 public。 bar
仍然可以访问 foo
,因为它是来自 Person
.
您不能从 Pete
调用 foo
,因为它仅在 Person 中可见。
class Pete extends Person {
public function doSomething() {
//Works because bar itself calls foo
$this->bar();
}
}
如果您想直接从 Pete
调用 foo
或覆盖它,您需要声明它 protected
而不是 private
.