简体   繁体   English

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

[英]How to print multiple variable created inside function of class in php

I have follwing class in file " super.class.php " 我在文件“ 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";
        }
}
?>

I want to print each variable in my other file. 我想在我的其他文件中打印每个变量。 Note: Classes are called from spl_autoload_register 注意:spl_autoload_register调用类

When I am trying to print :- 当我尝试打印时:-

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

I am getting error :- Trying to get property of non-object in agent.php 我收到错误消息:- Trying to get property of non-object in agent.php

Update: I got it working via this 更新:我通过它工作了

Hope I am not doing any wrong below :- 希望我在下面没有做错任何事情:-

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

Thanks 谢谢

You need to add the variables into your class, like so: 您需要将变量添加到类中,如下所示:

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

And then set them with 然后用

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

You can get them with 你可以用

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

Ok, you seem to have misunderstood how functions work in PHP - the variables you have declared in your functions are local to the function - ie they only have a value inside the functions scope. 好的,您似乎误解了函数在PHP中的工作方式-在函数中声明的变量是函数的局部变量-即,它们在函数作用域内仅具有一个值。

Secondly, the notation $obj_super->loadme()->tilt will give the value of the property tilt of the return of loadme() since loadme() doesnt return anything - you are essentially doing this null->tilt . 其次,符号$obj_super->loadme()->tilt将给出loadme()返回值的属性tilt值,因为loadme()不返回任何内容-您实际上是在执行null->tilt Which likely causes all sorts of errors if you check your log. 如果您检查日志,可能会导致各种错误。

What is it you are trying to achieve here? 您要在这里实现什么?

Perhaps you can design a class like this 也许你可以设计一个这样的班级

<?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
    }
?>

Then invoke the getter functions 然后调用getter函数

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

if you want to call it the exact way you are calling it you can return the object itself from each function. 如果您想以正确的方式调用它,则可以从每个函数返回对象本身。 Try the following 尝试以下

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;

again as @TheNytangel suggested the following also will work with this class. 再次@TheNytangel建议以下内容也适用于该类。 if you want to suppress this behavior also you may need to redesign your functions as objects. 如果要抑制这种行为,还可能需要将函数重新设计为对象。

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

As requested - the answer from Johny Bravo. 根据要求-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'];

As per OP's code in fiddle with modification. 按照OP的代码进行修改。 I have removed the instance variables. 我删除了实例变量。 New fiddle here http://phpfiddle.org/main/code/5qe-hn6 这里的新提琴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'];
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM