pdo :: fetchclasStype没有寻找自动加载的类
在我的 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
根据评论发布解决方法,因为它可能会帮助某人寻求相同的问题。
在我的index.php文件中
在我的 factoryclass 中,我创建一个实例
更新了我的类以在构造函数中接受参数并在其中称为setter,所以在我的myclass1.php
中想要读取一行,基于行结果创建对象,
发送行结果设置属性并使用这些属性
显示页面内容而不是直接处理查询结果。
Posting the workaround based on comments as it might help someone looking for the same.
In my index.php file
In my factoryclass where I create an instance
Updated my class to accept parameters in constructor and called setter in it, so in my MyClass1.php
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.