一起创建两个类的实例

发布于 2024-08-16 16:08:51 字数 2273 浏览 4 评论 0原文

我试图显示两个列表:一个用于类别和品牌,但仅显示类别。当我删除类别代码时,就会显示品牌。是因为无法在同一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

国际总奸 2024-08-23 16:08:51

您可以在同一个 php 实例中实例化数十/数百/X 任何类型的对象。
使用调试器或添加更多调试(回显)代码来查找错误。

例如,使用两个类的虚拟实现,

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>

可以毫无问题地打印。

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'),
    );
  }
}

your code prints

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

without any problem.

短叹 2024-08-23 16:08:51

不,您甚至可以为同一个类创建任意数量的实例。确保您的两个类彼此独立地包含在脚本中。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文