【发布时间】:2012-03-09 10:46:20
【问题描述】:
我正在尝试为一个类创建一个类对象,但我不断收到一些未知错误。
这是“helper class”,它从XML 文件中获取内容
<?php
class helperClass {
private $name;
private $weight;
private $category;
private $location;
//Creates a new product object with all its attributes
function __construct(){}
//List products from thedatabase
function listProduct(){
$xmlDoc = new DOMDocument();
$xmlDoc->load("storage.xml");
print $xmlDoc->saveXML();
}
?>
}
在这里,我试图从helperClassclass 创建一个对象,并从helperClass 调用方法listProducts,但是如果我尝试实例化helperClass 的对象,它将无法工作的代码
<?php
//Working code...
class businessLogic {
private $helper = null;
public function __construct() {
}
public function printXML() {
$obj = new helperClass();
$obj->fetchFromXMLDocument();
// you probably want to store that new object somewhere, maybe:
$this->helper = $obj;
}
}
}
?>
在你们的帮助下,我想通了,这就是我想做的事
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title></title>
</head>
<body>
<?php
require 'businessLogic.php';
$obj = new businessLogic();
$obj->printXML();
?>
</body>
</html>
【问题讨论】: