pdo :: fetchclasStype没有寻找自动加载的类

发布于 2025-02-09 04:33:45 字数 1226 浏览 2 评论 0原文

在我的 index.php 中,

include 'autoload.php';
//sql code 
while ($result = $list->fetch(PDO::FETCH_CLASS | PDO::FETCH_CLASSTYPE)) {
    echo $class = get_class($result);  //shows stdClass when autoload.php is included
    echo $result->showAttributes();
}

目标是根据查询结果的第一列调用类。如果我包括以下每个类,而不是 autoLoad.php ,我会得到所需的结果。

include 'Myclass1.php';
include 'Myclass2.php';

我如何避免包括捆绑课程,并使用自动加载功能使PDO :: fetch_clasStype工作。

这是我的autoload.php

class Autoloader
{
    public static function register()
    {
        spl_autoload_register(function ($class) {
            $file = str_replace('\\', DIRECTORY_SEPARATOR, $class).'.php';
                require $file;           
        });
    }
}
Autoloader::register();

编辑 由于有关于调试自动加载的评论,因此我使用了fetch_class,我可以加载相应的类。问题是Fetch_ClasStype无法识别自动加载的类。我无法使用Fetch_Class,因为该类是动态决定的。我已经打印了结果集,第一列是要自动加载的类名称。

while ($result = $list->fetch(PDO::FETCH_CLASS, 'MyClass1')) {
    echo $class = get_class($result);  //shows MyClass1, MyClass2 depending on what is passed to FETCH_CLASS
    echo $result->showAttributes();
}

In my index.php

include 'autoload.php';
//sql code 
while ($result = $list->fetch(PDO::FETCH_CLASS | PDO::FETCH_CLASSTYPE)) {
    echo $class = get_class($result);  //shows stdClass when autoload.php is included
    echo $result->showAttributes();
}

The goal is to call the class based on first column from the query result. If I include the following each class instead of autoload.php, I get the desired result.

include 'Myclass1.php';
include 'Myclass2.php';

How can I avoid including bundles of classes and use autoload feature to make PDO::FETCH_CLASSTYPE work.

This is my autoload.php

class Autoloader
{
    public static function register()
    {
        spl_autoload_register(function ($class) {
            $file = str_replace('\\', DIRECTORY_SEPARATOR, $class).'.php';
                require $file;           
        });
    }
}
Autoloader::register();

EDIT
Since there were comments about debugging the autoload, I used FETCH_CLASS, I can load respective classes. The issue is FETCH_CLASSTYPE is not able to identify the classes from autoload. I cannot use FETCH_CLASS as the class is decided dynamically. I have printed the result set and first column is the class names to be autoloaded.

while ($result = $list->fetch(PDO::FETCH_CLASS, 'MyClass1')) {
    echo $class = get_class($result);  //shows MyClass1, MyClass2 depending on what is passed to FETCH_CLASS
    echo $result->showAttributes();
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

厌味 2025-02-16 04:33:45

根据评论发布解决方法,因为它可能会帮助某人寻求相同的问题。

在我的index.php文件中

include 'autoload.php';

$object = new Mainclass();
$list = $object->getList();

while ($row = $list->fetch()) {
    $obj = FactoryClass::subClass($row['type'],$row); //read row and instantiate type object
    echo $obj->showAttributes();
}

在我的 factoryclass 中,我创建一个实例

class FactoryClass
{
    public static function subClass($obj = '', $values = [])
    { 
        if (!empty($obj)) { 
            $class = $obj;
        } else {
            $class = 'sampleclass'; //fallback class
        }
        return new $class($values);
    } 
}

更新了我的类以在构造函数中接受参数并在其中称为setter,所以在我的myclass1.php

class MyClass1 extends AbstractClass
{
    public function __construct($values = [])
    {
        parent::__construct($values);
        if (!empty($values)) {
            //call setter functions of class attributes
        }
    }
    public function showAttributes()
    {
        //define the display calling getters
    }
}

中想要读取一行,基于行结果创建对象,
发送行结果设置属性并使用这些属性
显示页面内容而不是直接处理查询结果。

Posting the workaround based on comments as it might help someone looking for the same.

In my index.php file

include 'autoload.php';

$object = new Mainclass();
$list = $object->getList();

while ($row = $list->fetch()) {
    $obj = FactoryClass::subClass($row['type'],$row); //read row and instantiate type object
    echo $obj->showAttributes();
}

In my factoryclass where I create an instance

class FactoryClass
{
    public static function subClass($obj = '', $values = [])
    { 
        if (!empty($obj)) { 
            $class = $obj;
        } else {
            $class = 'sampleclass'; //fallback class
        }
        return new $class($values);
    } 
}

Updated my class to accept parameters in constructor and called setter in it, so in my MyClass1.php

class MyClass1 extends AbstractClass
{
    public function __construct($values = [])
    {
        parent::__construct($values);
        if (!empty($values)) {
            //call setter functions of class attributes
        }
    }
    public function showAttributes()
    {
        //define the display calling getters
    }
}

What I wanted is to read a row, create an object based on row result,
send row result to set the properties and use those properties
to display the page content instead of dealing with the query result directly.

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