引用实例化对象的方法的更好方法是什么?
What's a better way to refer to the methods of the instantiating object?
好的,所以我创建了一个简单的问题重现 运行,并且我测试了代码和它 运行s。我的问题是,当 rock 实例位于 paper 实例内部时,如何从 Rock->mb() 内部调用 Paper->important() 而不将实例化对象注入每个方法? 我是通过passing/injecting 把mypaper 的$this 变成rock 的方法来完成的。主要问题是只有一种 rock 方法需要它,所以我如何轻松地 访问实例化对象的方法而不将它们传递给每个函数 ,考虑到我是 运行宁许多功能?最后一个问题是,也许我将它们注入到每个方法中是否重要,它会使用额外的内存还是资源? 我应该通过引用传递 $this 吗?,它会节省内存吗?另外,当我传递未使用的额外参数时会发生什么?
<?php
class Rock{
public function ma($args){ //what happens when it gets injected into this function?
return $args." in ma";
}
public function mb($args,$context){ //do I have to inject it?
if($args=="args2"){
$context->important();
return "<br>".$args." in mb";
}
}
//50 other functions that DONT require paper->important()
}
class Paper{
public function __construct($vitalString){
$this->vitalString = $vitalString;
}
public function all(){
$list = ['ma'=>'args1','mb'=>'args2'];
$objRock = new Rock();
foreach($list as $meth=>$args){
if(method_exists($objRock,$meth)){
$response = $objRock->{$meth}($args,$this);
//can I avoid injecting the instantiating $this, into every function I call if only one needs it?
echo $response;
}
}
}
function important(){
echo "<br>".$this->vitalString;
}
}
$myPaper = new Paper("Super Duper");
$myPaper->all();
?>
这是输出
args1 in ma
Super Duper
args2 in mb
我会像您目前所做的那样进行构造函数注入而不是方法注入,见下文:
class Rock{
private $paper;
public function __construct($paper){
$this->paper = $paper;
}
public function ma($args){
return $args." in ma";
}
public function mb($args){ //do I have to inject it?
if($args=="args2"){
$this->paper->important();
return "<br>".$args." in mb";
}
}
//50 other functions that DONT require paper->important()
}
在 mb 方法中,我将你的调用从
更改为
$context->important();
到
$this->paper->important();
论文 class 现在看起来像下面这样:
class Paper{
public function __construct($vitalString){
$this->vitalString = $vitalString;
}
public function all(){
$list = ['ma'=>'args1','mb'=>'args2'];
$objRock = new Rock($this); //<-------------------
foreach($list as $meth=>$args){
if(method_exists($objRock,$meth)){
$response = $objRock->{$meth}($args); //<-----------------
//can I avoid injecting the instantiating $this, into every function I call if only one needs it?
echo $response;
}
}
}
function important(){
echo "<br>".$this->vitalString;
}
}
通过构造函数注入,您可以在任何地方使用注入的 class 而不必担心将其传递给每个方法,即使该方法不需要它也是如此。 (此外,拥有从未使用过的参数的函数会让人感到困惑。)除非论文 class 有大量属性来保存天文数量的数据,否则您不必担心内存问题。
构造函数注入方法也很方便,以防万一您决定添加需要使用论文的其他方法class——如果您需要,它就在那里。
顺便说一句,所有对象都自动通过引用传递——但这与构造函数注入方法无关。
好的,所以我创建了一个简单的问题重现 运行,并且我测试了代码和它 运行s。我的问题是,当 rock 实例位于 paper 实例内部时,如何从 Rock->mb() 内部调用 Paper->important() 而不将实例化对象注入每个方法? 我是通过passing/injecting 把mypaper 的$this 变成rock 的方法来完成的。主要问题是只有一种 rock 方法需要它,所以我如何轻松地 访问实例化对象的方法而不将它们传递给每个函数 ,考虑到我是 运行宁许多功能?最后一个问题是,也许我将它们注入到每个方法中是否重要,它会使用额外的内存还是资源? 我应该通过引用传递 $this 吗?,它会节省内存吗?另外,当我传递未使用的额外参数时会发生什么?
<?php
class Rock{
public function ma($args){ //what happens when it gets injected into this function?
return $args." in ma";
}
public function mb($args,$context){ //do I have to inject it?
if($args=="args2"){
$context->important();
return "<br>".$args." in mb";
}
}
//50 other functions that DONT require paper->important()
}
class Paper{
public function __construct($vitalString){
$this->vitalString = $vitalString;
}
public function all(){
$list = ['ma'=>'args1','mb'=>'args2'];
$objRock = new Rock();
foreach($list as $meth=>$args){
if(method_exists($objRock,$meth)){
$response = $objRock->{$meth}($args,$this);
//can I avoid injecting the instantiating $this, into every function I call if only one needs it?
echo $response;
}
}
}
function important(){
echo "<br>".$this->vitalString;
}
}
$myPaper = new Paper("Super Duper");
$myPaper->all();
?>
这是输出
args1 in ma
Super Duper
args2 in mb
我会像您目前所做的那样进行构造函数注入而不是方法注入,见下文:
class Rock{
private $paper;
public function __construct($paper){
$this->paper = $paper;
}
public function ma($args){
return $args." in ma";
}
public function mb($args){ //do I have to inject it?
if($args=="args2"){
$this->paper->important();
return "<br>".$args." in mb";
}
}
//50 other functions that DONT require paper->important()
}
在 mb 方法中,我将你的调用从
更改为$context->important();
到
$this->paper->important();
论文 class 现在看起来像下面这样:
class Paper{
public function __construct($vitalString){
$this->vitalString = $vitalString;
}
public function all(){
$list = ['ma'=>'args1','mb'=>'args2'];
$objRock = new Rock($this); //<-------------------
foreach($list as $meth=>$args){
if(method_exists($objRock,$meth)){
$response = $objRock->{$meth}($args); //<-----------------
//can I avoid injecting the instantiating $this, into every function I call if only one needs it?
echo $response;
}
}
}
function important(){
echo "<br>".$this->vitalString;
}
}
通过构造函数注入,您可以在任何地方使用注入的 class 而不必担心将其传递给每个方法,即使该方法不需要它也是如此。 (此外,拥有从未使用过的参数的函数会让人感到困惑。)除非论文 class 有大量属性来保存天文数量的数据,否则您不必担心内存问题。
构造函数注入方法也很方便,以防万一您决定添加需要使用论文的其他方法class——如果您需要,它就在那里。
顺便说一句,所有对象都自动通过引用传递——但这与构造函数注入方法无关。