PHP SimpleXML 未定义成员错误
我不明白为什么这是一个错误。一切对我来说都很有意义。 $logFile
是一个 SimpleXMLElement
,因此它们应该能够使用 getName() 方法。
错误:
致命错误:第 29 行在 C:\xampp\htdocs\objLogParser.php 中的非对象上调用成员函数 getName()
我已在代码中标记了该错误。
<?php
class objLogParser
{
private $fileName;
private $logFile;
//Constructor
public function __construct($varFileName)
{
$this->fileName = $varFileName;
//Load as string
$xmlstr = file_get_contents($varFileName);
$logFile = new SimpleXMLElement($xmlstr);
//Load as file
$logFile = new SimpleXMLElement($varFileName,null,true);
}
public function printNodes()
{
$this->printHelper($this->logFile,0);
}
public function printHelper($currentNode, $offset)
{
echo $this->offset($offset);
echo $currentNode->getName(); ////////////////////LINE 29 ERROR
if($currentNode->attributes()->count() > 0)
echo "({$currentNode->attributes()})";
echo " {$currentNode}";
foreach ($currentNode->children() as $child) {
echo "<br>";
printHelper($child, ($offset+1));
}
}
function offset($offset)
{
for ($i = 0; $i < $offset; $i++)
echo "_ ";
}
}
?>
I can't figure out why this is an error. Everything makes sense to me. $logFile
is a SimpleXMLElement
and theirfore should be able to use the getName() method.
Error:
Fatal error: Call to a member function getName() on a non-object in C:\xampp\htdocs\objLogParser.php on line 29
I've marked the error in my code.
<?php
class objLogParser
{
private $fileName;
private $logFile;
//Constructor
public function __construct($varFileName)
{
$this->fileName = $varFileName;
//Load as string
$xmlstr = file_get_contents($varFileName);
$logFile = new SimpleXMLElement($xmlstr);
//Load as file
$logFile = new SimpleXMLElement($varFileName,null,true);
}
public function printNodes()
{
$this->printHelper($this->logFile,0);
}
public function printHelper($currentNode, $offset)
{
echo $this->offset($offset);
echo $currentNode->getName(); ////////////////////LINE 29 ERROR
if($currentNode->attributes()->count() > 0)
echo "({$currentNode->attributes()})";
echo " {$currentNode}";
foreach ($currentNode->children() as $child) {
echo "<br>";
printHelper($child, ($offset+1));
}
}
function offset($offset)
{
for ($i = 0; $i < $offset; $i++)
echo "_ ";
}
}
?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
它应该是
$this->logFile->getName();
。本质上,将$logFile
替换为logFile
。It should be
$this->logFile->getName();
. Essentially, replace$logFile
withlogFile
.$this->logFile 从未被设置。 $logFile 在 __construct() 中设置,但从未实际使用。 $this->logFile 和 $logFile 不是同一个变量。 $logFile 仅限于 __construct() 的范围;您无法在该方法之外访问它。 $this->logFile 可以通过类中的任何方法访问。尝试将 __construct() 中的 $logFile 更改为 $this->logFile。
$this->logFile is never being set. $logFile is set in __construct(), but is never actually used. $this->logFile and $logFile aren't the same variable. $logFile is limited to the scope of __construct(); you can't access it outside that method. $this->logFile can be accessed by any method in the class. Try changing $logFile in __construct() to $this->logFile.