一起创建两个类的实例 [英] create instance of two classes together

查看:118
本文介绍了一起创建两个类的实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想显示两个列表:一个用于类别和品牌,但只显示类别。当我删除类别的代码,品牌正在显示。是因为它是不可能在同一个php页面中创建两个类的实例?
in index.php:

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>"; 
    }
 ?>

以下是类的代码:

<?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);

}

}

 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;
        }


推荐答案

/ X在同一个php实例中的任何类型的对象。

使用调试器或添加更多调试(echo)代码来查找错误。

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

class CategoryList {
  public $mCategory=null;
  public function init() {
    $this->mCategory = array(
      array('name'=>'Cat A'),
      array('name'=>'Cat B'),
    );
  }
}

class BrandList {
  public $mBrand=null;
  public function init() {
    $this->mBrand = array(
      array('name'=>'Brand A'),
      array('name'=>'Brand B'),
    );
  }
}

您的代码打印

<a href=''>Cat A<br/></a><a href=''>Cat B<br/></a><a href=''>Brand A<br/></a><a href=''>Brand B<br/></a>

没有任何问题。

这篇关于一起创建两个类的实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆