[英]How to print multiple variable created inside function of class in 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";
}
}
?>
我想在我的其他文件中打印每个变量。 注意:从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'];
?>
谢谢
您需要将变量添加到类中,如下所示:
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;
好的,您似乎误解了函数在PHP中的工作方式-在函数中声明的变量是函数的局部变量-即,它们在函数作用域内仅具有一个值。
其次,符号$obj_super->loadme()->tilt
将给出loadme()
返回值的属性tilt
值,因为loadme()
不返回任何内容-您实际上是在执行null->tilt
。 如果您检查日志,可能会导致各种错误。
您要在这里实现什么?
也许你可以设计一个这样的班级
<?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();
?>
如果您想以正确的方式调用它,则可以从每个函数返回对象本身。 尝试以下
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;
根据要求-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'];
按照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'];
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.