如何在php中打印在类函数内创建的多个变量

时间:2013-09-21 04:00:40

标签: php class

我在文件“super.class.php

中有以下课程
<?php    
class super {

        public function loadme() {
            $tilt = "I am tilted";
            $straight = "I am Straight";
            $round = "I am round";
            $sleep = "I am Sleeping";
        }

        public function fireme() {
            $hit = "I am hit";
            $bullet = "I got bullet";
            $gun = "Cover me! I am receiving Heavy Firing";
        }
}
?>

我想在我的其他文件中打印每个变量。 注意:spl_autoload_register

调用类

当我尝试打印时: -

<?php
    $obj_super = new super();
    echo $obj_super->loadme()->tilt;
    echo $obj_super->fireme()->hit;
?>

我收到错误: - Trying to get property of non-object in agent.php

更新:我通过此

开始工作

希望我在下面没有做错: -

<?php    
    class super {

        public $title = array();
        public $situation = array();

        public function loadme() {
            $this->title['tilt'] = "I am tilted";
            $this->title['straight'] = "I am Straight";
            $this->title['round'] = "I am round";
            $this->title['sleep'] = "I am Sleeping"; 
            return $this->title;
        }

        public function fireme() {
            $this->situation['hit'] = "I am hit";
            $this->situation['bullet'] = "I got bullet";
            $this->situation['gun'] = "Cover me! I am receiving Heavy Firing";
            return $this->situation;
        }
    }
    ?>

    <?php
        $obj_super = new super();
        $value = $obj_super->loadme();
        echo $value['tilt'];
    ?>

由于

6 个答案:

答案 0 :(得分:2)

您需要将变量添加到您的类中,如下所示:

class super {
    public $tilt;
    public $straight;
}

然后用

设置它们
public function loadme(){
    $this->tilt = "I am tilted";
    $this->straight = "I am straight";
}

您可以使用

获取它们
$new = new super;
$new->loadme();
echo $new->tilt;

答案 1 :(得分:2)

好吧,你好像误解了函数在PHP中是如何工作的 - 你在函数中声明的变量是函数的局部变量 - 即它们只在函数范围内有一个值。

其次,符号$obj_super->loadme()->tilt将给出tilt返回的属性loadme()的值,因为loadme()不返回任何内容 - 您基本上是这样做的null->tilt 1}}。如果您检查日志,可能会导致各种错误。

你想在这里实现什么目标?

答案 2 :(得分:1)

也许你可以设计一个像这样的课程

<?php    
    class super {
        private $_tilt; 
        private $_straight;
        private $_round;
        private $_sleep;

        private $_hit;
        private $_bullet;
        private $_gun;

        public function loadme() {
            $this->_tilt = "I am tilted";
            $this->_straight = "I am Straight";
            $this->_round = "I am round";
            $this->_sleep = "I am Sleeping";
        }

        public function fireme() {
            $this->_hit = "I am hit";
            $this->_bullet = "I got bullet";
            $this->_gun = "Cover me! I am receiving Heavy Firing";
        }

        public function getTilt() {
            return $this->_tilt;
        }

        // Other getter functions
    }
?>

然后调用getter函数

<?php
    $oSuper = new super;
    $oSuper->loadme();
    echo $oSuper->getTilt();
?>

答案 3 :(得分:1)

如果你想以你调用它的确切方式调用它,你可以从每个函数返回对象本身。请尝试以下

class super {
    public $tilt = '';
    public $straight = '';
    public $round = '';
    public $sleep = '';
    public $hit = '';
    public $bullet = '';
    public $gun = '';
    public function loadme() {
        $this -> tilt = "I am tilted";
        $this -> straight = "I am Straight";
        $this -> round = "I am round";
        $this -> sleep = "I am Sleeping";
        return $this;
    }

    public function fireme() {
        $this -> hit = "I am hit";
        $this -> bullet = "I got bullet";
        $this -> gun = "Cover me! I am receiving Heavy Firing";
        return $this;
    }

}

$obj_super = new super();
echo $obj_super -> loadme() -> tilt;
echo $obj_super -> fireme() -> hit;

再次@TheNytangel建议以下内容也适用于本课程。如果你想抑制这种行为,你可能需要将你的函数重新设计为对象。

$obj_super = new super();
$obj_super -> loadme();
echo $obj_super -> tilt;
$obj_super -> fireme();
echo $obj_super  -> hit;

答案 4 :(得分:1)

根据要求 - 约翰尼布拉沃的答案。

class super {

    public $title = array();
    public $situation = array();

    public function loadme() {
        $this->title['tilt'] = "I am tilted";
        $this->title['straight'] = "I am Straight";
        $this->title['round'] = "I am round";
        $this->title['sleep'] = "I am Sleeping"; 
        return $this->title;
    }

    public function fireme() {
        $this->situation['hit'] = "I am hit";
        $this->situation['bullet'] = "I got bullet";
        $this->situation['gun'] = "Cover me! I am receiving Heavy Firing";
        return $this->situation;
    }
}


$obj_super = new super();
$value = $obj_super->loadme();
echo $value['tilt'];

答案 5 :(得分:0)

根据OP的代码改编。我删除了实例变量。这里的新小提琴http://phpfiddle.org/main/code/5qe-hn6

<?php    
class super {

    public function loadme() {
        $title=array();
        $title['tilt'] = "I am tilted";
        $title['straight'] = "I am Straight";
        $title['round'] = "I am round";
        $title['sleep'] = "I am Sleeping"; 
        return $title;
    }

    public function fireme() {
        $situation=array();
        $situation['hit'] = "I am hit";
        $situation['bullet'] = "I got bullet";
        $situation['gun'] = "Cover me! I am receiving Heavy Firing";
        return $situation;
    }
}
?>

<?php
    $obj_super = new super();
    $value = $obj_super->loadme();
    echo $value['tilt'];
?>