问题描述
_execute()的方法的请求对象。在该方法中,我(除其他事项外)创建一个名为pageController的对象,在其上调用一个方法,然后使用以下代码删除该对象:
I'm working on a simple PHP framework as a learning project. I've got a request object with a method called _execute()
. In that method I (among other things) create an object called pageController, call a method on it, and remove the object using the following code:
$controller = new $this->_controllerName($this);
call_user_func(array($controller, $this->_methodName));
unset($controller);
如您所见,我将当前对象传递给新的pageController的构造函数。我的构造函数如下:
As you can see I pass the current object to the constructor of the new pageController. My constructor is as follows:
public function __construct(Request $request) {
parent::__construct($request);
// More stuff
}
父母的控制者是像这样:
The parent's controller is like this:
public function __construct(Request $request) {
$this->_request = $request;
}
这一切都很好,但是我的析构函数有问题。在pageController中,我还有另外两种方法:
This all works fine, but there is a problem with my destructor. In the pageController I've also got two other methods:
public function __destruct() {
$this->_render();
}
public function _render($templateName = 'default') {
$this->_request->_response->_body = $this->_template->_render();
}
我的 _render()
如果我从pageController的另一个方法中调用该方法,则效果很好:然后,我可以使用 $ this-> _response-> _ body
。但是,当我从析构函数中调用 _render()
方法时,请求对象中的更改不会更改。当我在调用 _render()
之后立即调用 print_r()
时,更改是可见...
My _render()
method works great if I call it from within another method in pageController: I can then get the response body from the initial request object using $this->_response->_body
. When I call the _render()
method from my destructor though, the changes are not changed in the request object. When I call print_r()
right after the call to _render()
, the changes are somehow visible...
总结:我对 _request
所做的任何更改初始请求对象中的析构函数中的property不会以某种方式更改,因为该对象(几乎)始终不被复制而是被引用,因此引用该对象。我在做什么错?
Summarized: Any changes I make to the _request
property in the destructor are somehow not changed in the initial request object, which references to the same, since objects are (almost) always not copied but referenced. What am I doing wrong?
注意:我在,但是这些问题不够具体(因为当时我还没有完全理解问题,而且由于我自己进行了一些错误的测试)。我认为我应该问一个新的,具体的,直接的问题,以便有人希望能帮助我。
Note: I asked a similar question before here, but that questions was not specific enough (because I didn't fully understand the problem then and thanks to some bad testing by myself). I figured I should ask a new, specific, direct question so someone can hopefully help me out.
推荐答案
您使用的是哪个PHP版本使用?我无法使用以下代码在 5.3.6
上复制您的问题:
What PHP version are you using? I was not able to duplicate your issue on 5.3.6
with the following code:
class Foo {
public function __construct(Bar $bar) {
$this->bar = $bar;
}
public function __destruct() {
$this->bar->value = 'set by Foo::__destruct';
}
}
class Bar {
public function __construct() {
$this->value = 'set by Bar::__construct';
}
}
$bar = new Bar();
$foo = new Foo($bar);
print $bar->value . PHP_EOL; // => 'set by Bar::__construct'
unset($foo);
print $bar->value . PHP_EOL; // => 'set by Foo::__destruct'
这与您尝试执行的操作相同。如果是……听起来您的应用程序逻辑中的其他部分可能正在干扰。
Is that along the same lines as what you are attempting to do. If it is... it sounds like maybe some other part of you application logic is interfering.
这篇关于对象__destruct()中的修改未保存PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!