dousi1875 2011-10-31 22:40
浏览 22

php __autoload功能

I am using the following code to load my object classes;

function __autoload($class_name) {
    require_once ( 'classes/'.$class_name.'.class.php');
}

If would like it so that if it can't find the file, it goes to the Database, finds the table of the data item i'm trying to access. And creates a standard object on the stop which contains the variables which represent the columns in the DB. Is this possible? I know how to check if the file exists with PHP, but I'm not quite sure how I would return the new standard object once initiated with the correct variable names.

Many thanks.

  • 写回答

1条回答 默认 最新

  • duanre1891 2011-11-03 00:27
    关注

    Create a function that returns all column names for a table as an array (no database specs given, maybe MySQL?). Then initialize an object with empty values (NULL) for each of those:

    $names = get_table_column_names($table);
    $values = array_fill(0, count($names), NULL);
    $obj = (object) array_combine($name, $values);
    

    $obj is a standard PHP object containing those columns.

    However, this won't work with autoload, as you need to create the class definition on the fly to reflect the type. I would say, you need to inject it via eval/include with public properties initialized to NULL by default.

    评论

报告相同问题?

悬赏问题

  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本