如何在php中打印类函数内部创建的多个变量
我在文件“如何在php中打印类函数内部创建的多个变量,php,class,Php,Class,我在文件“super.class.php”中有以下类 我收到错误:-试图在agent.php中获取非对象的属性 <?php class super { public function loadme() { $tilt = "I am tilted"; $straight = "I am Straight"; $round = "I am round"; $sleep =
super.class.php
”中有以下类
我收到错误:-试图在agent.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";
}
}
?>
更新:我通过这个
希望我没有做错以下事情:-
<?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;
你可以用它来买
public function loadme(){
$this->tilt = "I am tilted";
$this->straight = "I am straight";
}
$new = new super;
$new->loadme();
echo $new->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;
你可以用它来买
public function loadme(){
$this->tilt = "I am tilted";
$this->straight = "I am straight";
}
$new = new super;
$new->loadme();
echo $new->tilt;
好的,您似乎误解了函数在PHP中的工作方式-您在函数中声明的变量是函数的本地变量-即,它们只有函数范围内的值 其次,符号
$obj\u super->loadme()->tilt
将给出loadme()
返回的属性tilt
的值,因为loadme()
不返回任何内容-您实际上是在执行此操作null->tilt
。如果查看日志,可能会导致各种错误
您想在这里实现什么?好的,您似乎误解了函数在PHP中的工作方式-您在函数中声明的变量是函数的局部变量-即,它们只有函数范围内的值 其次,符号
$obj\u 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();
?>
也许您可以设计这样的类
<?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;
如果您想以调用它的确切方式调用它,可以从每个函数返回对象本身。试试下面的方法
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;
根据要求-约翰尼·布拉沃的回答
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'];
根据要求-约翰尼·布拉沃的回答
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的代码进行修改。我已经删除了实例变量。新小提琴
根据OP的代码修改。我已经删除了实例变量。新小提琴
所有这些都是局部变量,它们没有附加到实例$this->tilt=…
,等等……我甚至尝试在函数内部使用public$variable
而不是global$variable
,它仍然不起作用:(如果你真的想这样做,你需要在每个方法中创建一个新对象(例如stdClass
)然后将局部变量作为属性分配给它,然后返回该对象。这非常混乱-你到底想做什么?避免全局。这是纯粹的邪恶。约翰尼,不。@elclars我认为将它们作为公共属性会更糟。目前它看起来像loadme和fireme应该是一个单独的类。所有这些se是局部变量,它们没有连接到实例。$this->tilt=…
等等……我甚至尝试在函数内部使用public$variable
而不是global$variable
,它仍然不起作用:(如果你真的想这样做,你需要在每个方法中创建一个新对象(例如stdClass
)然后将局部变量作为属性分配给它,然后返回该对象。这很混乱-你到底想做什么?避免全局。这是纯粹的邪恶。Johny,不。@elclars我认为将它们作为公共属性会更糟。目前它看起来像loadme和fireme,它们应该是一个单独的类吗你的意思是return
的属性
?你在哪里读到的?它是函数定义为方法的对象的属性,即$this->property
。如果我在方法中返回一个对象,那么使用->操作符,我访问我返回的对象的属性。我可以从函数返回多个变量吗?@ZackNewsham抱歉误解了这一点。您能添加一个示例吗?仅在对象或数组中,这样您就可以执行此操作$ret=new stdClass();$ret->variable=“value”;return$ret;return
的property
是什么意思?你在哪里读到的?它是对象的属性,函数在其中定义为方法,即$this->property
。如果我在方法中返回一个对象,那么使用->操作符,我访问我返回的对象的属性。我可以返回多个吗来自函数的变量?@ZackNewsham抱歉误解了这一点。您能添加一个示例吗?仅在对象或数组中,这样您就可以执行$ret=new stdClass();$ret->variable=“value”;return$ret;我只需要在函数的帮助下调用它:(如果您在函数中设置它们,如public function(){$this->tilt='which';}
然后您可以执行$new->function()
然后echo$new->tilt;
我通过这个让它工作起来了。希望我没有做错任何事。如果它是正确的,可以有人发布这个,这样我就可以接受作为答案。我无法添加我的答案,因为我的声誉不高。我只需要在函数的帮助下调用它:(如果你在