将参数从一种方法传递给另一种方法

马可:

我有这个:

class Passing
{
    public function foo( $firstArg, $secondArg )
    {
        echo 'First argument ' . $firstArg;

        $passing->bar( 'second arg' );
    }
    
    public function bar()
    {
        echo 'Second argument ' . $secondArg;
    }
}
$passing = new Passing();

我想传递$secondArgfoo()方法。

例如:

$passing->foo( 'first arg', 'second arg' );

如果可能的话?

OpenCadaver:

可能有几种方法,但是您可以将其分配为对象变量:

class Passing
{
    public $something;  // maybe use private or protected

    public function foo( $firstArg, $secondArg )
    {
        $this->something = $secondArg;
        echo 'First argument ' . $firstArg;
    }
    
    public function bar()
    {
        echo 'Second argument ' . $this->something;
    }
}
$passing = new Passing();

$passing->foo( 'first arg', 'second arg' ); // first arg
$passing->bar(); // second arg

您必须正确地构造它,以防在调用etc等bar()之前使用或设置默认值;`或多种方式。foo()bar()isset($this->something)public $something = 'blank

很多时候,您可能想在构造函数中执行此操作:

public function __construct( $firstArg, $secondArg )
{
    $this->something  = $firstArg;
    $this->something2 = $secondArg;
}

然后可以通过其他方法使用它们:

$passing = new Passing( 'first arg', 'second arg' );

$passing->foo(); // first arg
$passing->bar(); // second arg 

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何将值从一种方法传递给另一种方法

将值参数从一种方法传递到另一种方法

如何将一种方法作为参数传递给另一种方法?

将参数值从一种方法传递到另一种aspx.net

将方法作为参数传递给Java中的另一种方法

将方法从一种方法传递到另一种方法时的逻辑

如何将变量从一种方法传递到另一种方法?

如何将值从一种方法传递到另一种方法?

无法将某些字段从一种方法传递到另一种方法

将值从一种方法传递到另一种方法

如何将一种方法的返回值传递给另一种方法?

如何将一种方法的“自我”价值传递给另一种方法?

JavaScript:将未知数量的参数从一种方法传递到另一种方法

将一种测试方法的输出传递给另一种方法testng

使用 List 从一种方法到另一种方法

将一个方法参数传递给另一种方法-C#

在角度 13 中将值从一种方法传递到另一种方法

如何在scrapy中将信息从一种方法传递到另一种方法

非类型模板参数:传递给另一种方法

如何从一种方法调用参数到另一种方法?

从一种方法传递给另一种方法时变量不一致

将类传递给另一种方法

如何将局部变量的值传递给另一种方法

将varargs作为不同类型传递给另一种方法

将数据传递给另一种方法

如何将代码生成的控件传递给另一种方法

将KDoc文档从一种方法复制到另一种方法

如何将int值从一种方法转换为另一种方法

将值从一种方法返回到另一种方法