一起创建两个类的实例
我试图显示两个列表:一个用于类别和品牌,但仅显示类别。当我删除类别代码时,就会显示品牌。是因为无法在同一个 php 页面中创建两个类的实例吗? 在index.php中:
<?php
$obj = new CategoryList();
if (method_exists($obj, 'init'))
{
$obj->init();
}
for($i = 0;$i< count($obj->mCategory); $i++)
{
echo "<a href=''>";
echo $obj->mCategory[$i]['name']. "<br/>";
echo "</a>";
}
$obj2 = new BrandList();
if (method_exists($obj2, 'init'))
{
$obj2->init();
}
for($i = 0;$i< count($obj2->mBrand);$i++)
{
echo "<a href=''>";
echo $obj2->mBrand[$i]['name']. "<br/>";
echo "</a>";
}
?>
这是类的代码:
$mSelectedCategory = (int)$_GET['category_id']; } public function init() { $this->mCategory = Catalog::GetCategory(); } } ?><?php
class BrandList
{
public $mSelectedBrand = 0;
public $mBrand;
public function __construct()
{
if (isset ($_GET['brand_id']))
$this->$mSelectedBrand = (int)$_GET['brand_id'];
}
public function init()
{
$this->mBrand = Catalog::GetBrand();
}
}
?>
也许这可能会有所帮助:
class Catalog
{
//get id and name of category
public static function GetCategory()
{
$sql = 'CALL catalog_get_category_list()';
return DatabaseHandler::GetAll($sql);
}
public static function GetBrand()
{
$sql = 'CALL catalog_get_brands_list()';
return DatabaseHandler::GetAll($sql);
}
}
在DatabaseHandler类中:
public static function GetAll($sqlQuery, $params = null, $fetchStyle = PDO::FETCH_ASSOC)
{
$result = null;
try
{
$database_handler = self::GetHandler();
$statement_handler = $database_handler->prepare($sqlQuery);
$statement_handler->execute($params);
$result = $statement_handler->fetchAll($fetchStyle);
}
catch(PDOException $e)
{
self::Close();
trigger_error($e->getMessage(), E_USER_ERROR);
}
return $result;
}
Im trying to display two list: one for categories and brand but only the categories are being displayed. And when I remove the code for categories, the brands are being displayed. Is it because it is not possible to create instances of two classes in the same php page?
In index.php:
<?php
$obj = new CategoryList();
if (method_exists($obj, 'init'))
{
$obj->init();
}
for($i = 0;$i< count($obj->mCategory); $i++)
{
echo "<a href=''>";
echo $obj->mCategory[$i]['name']. "<br/>";
echo "</a>";
}
$obj2 = new BrandList();
if (method_exists($obj2, 'init'))
{
$obj2->init();
}
for($i = 0;$i< count($obj2->mBrand);$i++)
{
echo "<a href=''>";
echo $obj2->mBrand[$i]['name']. "<br/>";
echo "</a>";
}
?>
Here's the code for the classes:
$mSelectedCategory = (int)$_GET['category_id'];
}
public function init()
{
$this->mCategory = Catalog::GetCategory();
}
}
?>
<?php
class BrandList
{
public $mSelectedBrand = 0;
public $mBrand;
public function __construct()
{
if (isset ($_GET['brand_id']))
$this->$mSelectedBrand = (int)$_GET['brand_id'];
}
public function init()
{
$this->mBrand = Catalog::GetBrand();
}
}
?>
Maybe this might help:
class Catalog
{
//get id and name of category
public static function GetCategory()
{
$sql = 'CALL catalog_get_category_list()';
return DatabaseHandler::GetAll($sql);
}
public static function GetBrand()
{
$sql = 'CALL catalog_get_brands_list()';
return DatabaseHandler::GetAll($sql);
}
}
in DatabaseHandler class:
public static function GetAll($sqlQuery, $params = null, $fetchStyle = PDO::FETCH_ASSOC)
{
$result = null;
try
{
$database_handler = self::GetHandler();
$statement_handler = $database_handler->prepare($sqlQuery);
$statement_handler->execute($params);
$result = $statement_handler->fetchAll($fetchStyle);
}
catch(PDOException $e)
{
self::Close();
trigger_error($e->getMessage(), E_USER_ERROR);
}
return $result;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以在同一个 php 实例中实例化数十/数百/X 任何类型的对象。
使用调试器或添加更多调试(回显)代码来查找错误。
例如,使用两个类的虚拟实现,
您的代码
可以毫无问题地打印。
You can instantiate dozens/hundreds/X of objects of any kind within the same php instance.
Use a debugger or add more debug (echo) code to find the error.
e.g. using this dummy implementation of the two classes
your code prints
without any problem.
不,您甚至可以为同一个类创建任意数量的实例。确保您的两个类彼此独立地包含在脚本中。
No, you can create any number of instances even for the same class. Make sure that your both classes are included in your script independently of each other.