doujuanju3076 2010-07-06 15:27 采纳率: 0%
浏览 70
已采纳

需要模型数据库类(PHP / MySQL)

I'm after a model-based database class for PHP/MySQL. But it must have one particular functionality:

Once the class is initialised ( e.g $user=new User(); ) I should be able to access the attributes in the user table as follows:

$user->name // returns the value of the 'name' field in the user table

I should not have to define the attributes or need to create any functions to return the attribute value, it should be done automatically when the class is initialised.

Is there such a database class out there? If not can someone tell me how I would go about creating this functionality?

  • 写回答

4条回答 默认 最新

  • doupang3062 2010-07-06 15:30
    关注

    I am unsure on a database class, but if you were to write one then look into PHP's Magic Methods, in particular the __get() function

    http://www.php.net/manual/en/language.oop5.overloading.php#language.oop5.overloading.members

    class myClass {
    
        function __get($field) {
            // grab $field from database
        }
    
        function __set($field, value) {
            // set $field in database
        }
    }
    
    $class = new myClass();
    
    echo $class->field1; // grab value field1
    
    $class->field1 = 'myVal'; // set field1 = 'myVal';
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题