1

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'];
    ?>

谢谢

4

6 回答 6

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;
于 2013-09-21T04:03:33.510 回答
2

好的,您似乎误解了函数在 PHP 中的工作方式 - 您在函数中声明的变量是函数的本地变量 - 即它们仅在函数范围内具有值。

其次,该符号$obj_super->loadme()->tilt将给出tiltreturn的属性的值,loadme()因为它loadme()不返回任何东西——你实际上是在这样做null->tilt。如果您检查日志,这可能会导致各种错误。

你想在这里实现什么?

于 2013-09-21T04:04:17.300 回答
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();
?>
于 2013-09-21T04:13:07.720 回答
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;
于 2013-09-21T04:13:51.073 回答
1

根据要求 - Johny Bravo 的回答。

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'];
于 2013-09-21T04:51:07.443 回答
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'];
?>
于 2013-09-21T05:03:25.183 回答