【问题标题】:PHP class constructor to return mysql row as object [closed]PHP类构造函数将mysql行作为对象返回[关闭]
【发布时间】:2012-07-28 21:29:40
【问题描述】:

有人可以提供一个如何实现这一目标的示例吗?我希望像这样使用它:

$user = new User($id = '5');
echo "hi {$user->firstName}\n";

对象应包含行中的所有字段,这些字段可能会发生变化。

提前致谢!

【问题讨论】:

  • 我认为需要更多信息。您希望输出来自 __construct?
  • 是的,更具体地说,'User' 类的构造函数是什么样的?

标签: php mysql oop class constructor


【解决方案1】:

我在这里创建逻辑原型

class user 
{
    protected $_table = 'tbl_user';
    protected $_db;       // stores the database handler
    public $_user;     // stores the user data

    protected function __construct($user_id)
    {
          $stmt = "SELECT * FROM $this->_table WHERE id='".(int)$user_id."'";
          if ($this->_db->query($stmt)){
              $fetch = $this->_db->mysql_query($stmt);
              $this->_user = mysql_fetch_row($fetch);
          }
    }    

}

 $obj_user=new user($user_id);
 // now you can access 
 $obj_user->_user->firstname

【讨论】:

    【解决方案2】:

    你必须先创建一个简单的 php 类命名 User。 正确封装它。 将所有 getter 和 setter 写入此。

    例如。

    class User
    {
    private $firstName = '';
    public function getfirstName($firstName)
    {
    return $firstName;
    }
    public function setfirstName($firstName)
    {
    this.firstName = $firstName;
    }
    }
    
    //How to use it.
    
    $user = new User();
    $user->serfirstName('Suryansh');
    echo 'Hi '.$user->getfirstName();
    

    【讨论】:

      【解决方案3】:

      例如(非常简化 - 显示其背后的想法)...

      <?php
      class User {
        /**
         * @var PDO
         **/
        protected $_dbh;
      
        protected $_userid;
        protected $_firstname;
        protected $_surname;
      
        /**
         * init database connection
         *
         * @return void
         **/
        private function _initDatabase() {
          try {
            $this->_dbh = new PDO('mysql:...');
          } catch(PDOException $e) {
            die('not able to connect: '. $e->getMessage());
          }
        }
      
        /**
         * load user data from database
         *
         * @return void
         **/
        private function _loadData() {
          $this->_initDatabase();
      
          // create sql statement and select data
          $SQL = "
            SELECT *
            FROM   users
            WHERE  userid = :userid
          ";
      
          $dbStatement = $this->_dbh->prepare($SQL);
          $dbStatement->bindParam(':userid', $this->_userid, PDO::PARAM_INT);
          $dbStatement->execute();
      
          // assign data
          $userdata = $dbStatement->fetch(PDO::FETCH_ASSOC);
          $this->_firstname = $userdata['firstname'];
          $this->_surname   = $userdata['surname'];
          // ... and so on
        }
      
        /**
         * construct
         *
         * @param  int $userid
         * @return void
         **/
        public function __construct($userid) {
          $this->_userid = intval($userid);
      
          $this->_loadData();
        }
      
        /**
         * overload __get
         *
         * @param string $key
         * @return mixed
         **/
        public function __get($key) {
          if(property_exists($this, '_'. $key)) {
            return $this->{'_'. $key};
          }
        }
      }
      
      $user = new User(5);
      echo('Hi '. $user->firstname);
      ?>
      

      【讨论】:

      • 嗨,我刚开始学习面向对象的 php,我想知道您在函数和变量名称中使用前导下划线是否有原因?谢谢。
      • @Iciamp:前面的下划线是早期编程的某种遗留物。这用于告诉变量或函数名称的范围是private(或protected - 早期没有protected 用于php 的关键字)。现在,我不再将前导下划线用于此类目的。
      • 啊,我明白了。很好,谢谢你的回答。
      猜你喜欢
      • 1970-01-01
      • 2016-07-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多