0

所以我有两个课程:

表类

<?php
    class table {
        protected $id = null;
        protected $table = null;

        function __construct() {

        }

        function bind($data) {
            // print_r($data);
            foreach ($data as $key=>$value) {
               $this->key = $value;
               //   echo $key."--".$value;
               //     echo $this->$key;
            }
        }
   }
?>

用户类

<?php
    class user extends table
    {
        var $username = null;
        var $password = null;
        var $email = null;
        var $table = "user";
    }
?>

我也有一个索引引导程序......

<?php
    include('table.class.php');
    include('user.class.php');

    $user = new user();
    $data = array("username" => "Forest", "password" => "*****",  "email"=>"foo@bar.com");    
    $user->bind($data);
    $classVars = get_class_vars(get_class($user));
    print_r($classVars);

?>

它应该返回:

Array(
    [username] => Forest,
    [password] => *******,
    [email]=>foo@bar.com
    [table] => user
)

INSTEAD 它返回:

Array (
    [username] =>
    [password] =>
    [email] =>
    [table] => user

)

有人可以告诉我为什么变量没有绑定到超类吗??????

根据这里它应该工作:

http://codeslayer2010.wordpress.com/2012/04/08/developer-journal-2012-03-30-building-a-php-database-connection-class-from-scratch-singleton-activerecord/

4

2 回答 2

1

在你使用foreach而不是.bind$this->key = $value$this->{$key} = $value

并获取实例变量(不是类默认值)使用get_object_vars().

于 2013-04-27T07:51:27.007 回答
0

它应该输出:

不,不应该。get_class_vars返回类中定义的变量,您正在寻找实例化的对象属性,因此您应该使用:

$objectVars = get_object_vars($user);
于 2013-04-27T07:50:00.100 回答