php - Call to a member function prepare() What's wrong with this? OOP -


okay have class (will include header, , first function).

require_once("./inc/db.inc.php");     class users {        /**     * properties     **/      private $insert;      protected $user;      protected $email;      protected $get;      protected $check;      protected $generate;      protected $drop;       /**     * public function register     *     * registers user system, checking errors.     * if error found, throw new exception.     *     * @parm username username user posted.     * @parm password password user posted.     * @parm repassword validated password user posted.     * @parm email email user posted.     * @parm reemail validated email user posted.     * @parm day day user posted (for date of birth).     * @parm month month user posted (for date of birth).     * @parm year year user posted (for date of birth).     *     * @return return true means correct, register successfully.     **/      public function register($username, $password, $repassword, $email, $reemail, $day, $month, $year)     {          global $pdo;          // check if passwords matching.         if ($password != $repassword)         {             throw new exception ("passwords not match.");         }         // check if emails matching.         else if ($email != $reemail)         {             throw new exception ("emails not match.");         }          // main insert query         $this->insert = $pdo->prepare         ("             insert users             (user_name, user_password, user_email, user_birth)             values             (:username, :password, :email, :birth)         "); ... , on... ^ error there 

for reason getting error

fatal error: call member function prepare() on non-object in c:\xampp\htdocs\drip\class\users.class.php on line 68 

it worked fine before, starting doign after converted use autoload classes.

registration page:

include ("inc/config.inc.php");  $users = new users; 

and that's how use function register (error happens here):

    try     {         $users->register($_post['user'], $_post['pass'], $_post['repass'], $_post['email'], $_post['reemail'], $_post['day'], $_post['month'], $_post['year']);         echo 'successfully registered!';     }     catch (exception $e)     {         echo $e->getmessage();     } 

i can't think of anything.

i including db.inc.php, has db connection in var $pdo object, php says it's not?...

        $pdo = new pdo('mysql:host='.mysql_host.';dbname=driptone', mysql_user, mysql_password);                  try         {             $pdo->setattribute(pdo::attr_errmode, pdo::errmode_exception);         }          /**         * connection failed, print error.         * @var e holds error message.         **/          catch(pdoexception $e)         {             echo $e->getmessage();         } 

what did wrong? why that? lot.

and that's how auto load:

function classautoload($class) {     if (file_exists("class/$class.class.php"))         include("class/".$class.".class.php"); }  spl_autoload_register('classautoload'); 

the best way $pdo object needs dependency injection. this.

class users {     private $insert;     ...     private $pdo;      public function __construct($pdo) {         $this->pdo = $pdo;     }      public function register(...) {         ...         $this->pdo->prepare(...);         ...     } } 

then in registration page

include ("inc/config.inc.php"); include ("inc/db.inc.php"); $users = new users($pdo); 

Comments

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

android - send complex objects as post php java -

charts - What graph/dashboard product is facebook using in Dashboard: PUE & WUE -