PHP(OOP):如何在每个方法中调用新方法
假设我有一节课PHP(OOP):如何在每个方法中调用新方法,php,oop,Php,Oop,假设我有一节课 <?php class MyClass extends OtherClass { public function foo() { // some stuff } public function bar() { // some stuff } public function baz() { // some stuff } } 我可以在每个方法中添加一个简单的$this->run
<?php
class MyClass extends OtherClass
{
public function foo()
{
// some stuff
}
public function bar()
{
// some stuff
}
public function baz()
{
// some stuff
}
}
我可以在每个方法中添加一个简单的
$this->run\u this()
,好的,但是是否可以添加一些“神奇”的接收者来从这个类的每个方法中调用run\u this()
?不完全确定您想要什么,但是如果您希望它对所有子对象都可用,我会将它作为一个受保护的函数放在其他类中。这样,只有子对象才能访问它
class OtherClass {
protected function run_this()
{
$mandatary = true;
return $mandatary;
}
}
class MyClass extends OtherClass
{
public function foo()
{
$mandatory = $this->run_this();
// some stuff
}
public function bar()
{
$mandatory = $this->run_this();
// some stuff
}
public function baz()
{
$mandatory = $this->run_this();
// some stuff
}
}
你不能使用构造函数吗
<?php
class MyClass extends OtherClass
{
function __construct() {
$mandatory = $this->run_this();
}
private function run_this()
{
$mandatary = true;
return $mandatary;
}
public function foo()
{
// some stuff
if ($this->mandatory) {
//true
} else {
//false
}
}
public function bar()
{
// some stuff
}
public function baz()
{
// some stuff
}
}
我仍然认为你在做一些事情,因为你在某处遇到了设计问题(),但是如果你坚持按照你的要求去做,你可以装饰这个类:
class OtherClass {}
class MyClass extends OtherClass
{
public function foo()
{
// some stuff
}
public function bar()
{
// some stuff
}
public function baz()
{
// some stuff
}
public function run_this()
{
$mandatary = true;
return $mandatary;
}
}
class MyClassDecorator
{
private $myClass;
public function __construct($myClass)
{
$this->myClass = $myClass;
}
public function __call($name, array $arguments)
{
// run the mthod on every call
$this->myClass->run_this();
// run the actual method we called
call_user_func_array ([$this->myClass, $name], $arguments)
}
}
$myClass = new MyClass();
$decorator = new MyClassDecorator($myClass);
// will first run the `run_this` method and afterwards foo
$decorator->foo();
// etc
//$decorator->bar();
这有点难说,上面的工作原理与您要求的一样,但是正如前面所述,这很可能不是您真正想要做的。如果函数是私有的,您不能拒绝。如果它是公共的,您可以装饰MyClass
类。另外,我觉得如果你需要这样做,你会遇到一些设计问题。我根本不需要私有,我可以毫无问题地声明公共(在我的场景中)…这不会解决问题,因为“run_this”只会运行一次(在对象实例上)。但是Sinerba希望每次调用对象上的方法时都能运行它。(?)啊,我读了这篇文章,因为他想在类实例上运行它,但希望每个方法都可以使用它:(当然,在这种情况下,解决方案就是在所有其他方法中调用第1行的方法吗?没有冒犯,这太棒了,但只是为了完成(也不是为了你,因为我相信你知道):这甚至可以在一个类中更容易地完成,而无需使用decorator模式(从而创建多个类和传递对象)。只是说。也许最好保持简单。
class OtherClass {}
class MyClass extends OtherClass
{
public function foo()
{
// some stuff
}
public function bar()
{
// some stuff
}
public function baz()
{
// some stuff
}
public function run_this()
{
$mandatary = true;
return $mandatary;
}
}
class MyClassDecorator
{
private $myClass;
public function __construct($myClass)
{
$this->myClass = $myClass;
}
public function __call($name, array $arguments)
{
// run the mthod on every call
$this->myClass->run_this();
// run the actual method we called
call_user_func_array ([$this->myClass, $name], $arguments)
}
}
$myClass = new MyClass();
$decorator = new MyClassDecorator($myClass);
// will first run the `run_this` method and afterwards foo
$decorator->foo();
// etc
//$decorator->bar();