将PHP类作为参数传递



如何在函数中将类作为参数传递

到目前为止,我已经尝试过

$sc = new SampleClass();
SampleFunction($sc);

function SampleFunction(&$refClass)
{
    echo $refClass->getValue();
}

这是我所做事情的一个简化例子。。实际上,我必须在这个示例函数中执行复杂的过程。我没有从示例函数得到任何响应。我做错了什么?谢谢

更新

char.php

   class Charss {
    var $name=0;
    var $hp=500;
    var $spd=10;
    var $rtime=10;
    var $dmg=10;
    function __construct( $name, $hp, $spd, $rtime , $dmg) { 
            $this->name = $name;
            $this->hp = $hp;
            $this->spd  = $spd;
            $this->rtime = $rtime;
            $this->dmg = $dmg;
        }
    function get_name() {
        return $this->name;
    }
    function set_name($new_name) {
        $this->name = $new_name;
    }
    function get_hp() {
        return $this->hp;
    }
    function set_hp($new_hp) {
        $this->hp = $new_hp;
    }
    function get_spd() {
        return $this->spd;
    }
    function set_spd($new_spd) {
        $this->spd = $new_spd;
    }
    function get_rtime() {
        return $this->rtime;
    }
    function set_rtime($new_rtime) {
        $this->rtime = $new_rtime;
    }
    function get_dmg() {
        return $this->get_dmg;
    }
    function set_dmg($new_dmg) {
        $this->dmg = $new_dmg;
    }
}

myclass.php

    require("char.php");
class Person {
function try_process()
{
    $chr1 = new Charss("Player1",500,3,0,50);
    $chr2 = new Charss("Player2",500,6,0,70);
    while ($chr1->get_hp() > 0 && $chr2->get_hp() > 0)
    {
        $sth = min($chr1->get_rtime(), $chr2->get_rtime());
        if ($chr1->get_rtime() == 0 && $chr2->get_rtime() > 0)
        {
            exit;
            Fight($chr1,$chr2);
            $chr1->set_rtime($chr1->get_spd());
        }
        elseif ($chr2->get_rtime() == 0 && $chr1->get_rtime() > 0)
        {
            Fight($chr2,$chr1);
            $chr2->set_rtime($chr2->get_spd());
        }
        else 
        {
            Fight($chr1,$chr2); #having trouble with this
            $chr1->set_rtime($chr1->get_spd());
        }
        $chr1->set_rtime($chr1->get_rtime() - $sth);
        $chr2->set_rtime($chr2->get_rtime() - $sth);
    }
}
function Fight($atk,$def)
{
    $def->set_hp($def->get_hp() - $atk->get_dmg());
    echo $atk->get_name() . " attacked " . $def->get_name() . " for " . $atk->get_dmg() . " damage";
}

}

所以我调用函数try_process on按钮点击

您在那里实际做的是传递一个对象,而不是一个类。

$sc = new SampleClass();

创建SampleClass的实例,也称为对象。

我认为有一些错误被抛到了其他地方,因为你所拥有的是正确的。我测试了以下代码,得到了预期的输出:

class SampleClass
{
    public function getValue()
    {
        return 4;
    }
}
$sc = new SampleClass();
SampleFunction($sc);
function SampleFunction(&$refClass)
{
    echo $refClass->getValue();
}

输出:4

如果您提供实际代码的更多详细信息,我们可能能够确定问题所在。

我看不出你的代码有什么问题

使用&然而,不推荐使用refClass,我想它将从PHP版本的未来迭代中删除

但这里有一个的例子

类对象是作为引用传递的,所以不需要"&"

http://ideone.com/GbmUy

为什么函数参数是引用?可能不应该。

除此之外,您发布的内容没有任何问题,因此错误很可能在SampleClass中。

其他人的回答很好,但这是一个愚蠢的小例子,向您展示如何修改类(通过调用属性设置器,或直接设置公共属性)

class foo {
    private $member1;
    public  $member2;
    public function __construct($member1,$member2) {
            $this->member1=$member1;
            $this->member2=$member2;
    }
    public function SetMember1($value) {
            $this->member1 = $value;
    }
    public function GetMember1() {
            return $this->member1;
    }
}
function SetMembers(foo $obj, $member1, $member2) {
    // Call a setter
    $obj->SetMember1($member1);
    // Set a member variable directly
    $obj->member2 = $member2;
}
$obj = new foo('default member 1', 'default member 2');
echo "member1 (before): {$obj->GetMember1()}n";
echo "member2 (before): {$obj->member2}n";
// Change values
SetMembers($obj, 'new member1', 'new member2');
echo "member1 (after): {$obj->GetMember1()}n";
echo "member2 (after): {$obj->member2}n";

这将输出:

member1 (before): default member 1
member2 (before): default member 2
member1 (after): new member1
member2 (after): new member2

相关内容

  • 没有找到相关文章

最新更新