Welcome Guest, Not a member yet? Register   Sign In
Message: Undefined variable: _forename but its there
#1

Hey, I am having a problem with a variable inside a class not being seen even though its there. Please help!

User Controller

public function create() {

        $params = array('x', 'xxx', 'xxxxx', 'xxxxxx', 'x', 'xx', 'xxxx');
        $this->load->library('User', $params);
    }


User Library


<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class User {
   public $_forename          = "";
   public $_surname           = "";
   public $_dob               = "";
   public $_address           = "";
   public $_telephone         = "";
   public $_email             = "";
   public $_password          = "";
   
   public function __construct($params) {

       $this->setForename($params[0]);
       $this->setSurname($params[1]);
       $this->setDOB($params[2]);
       $this->setAddress($params[3]);
       $this->setTelephone($params[4]);
       $this->setEmail($params[5]);
       $this->setPassword($params[6]);

   }

   public function getForename()   { return $this->$_forename; }
   public function getSurname()    { return $this->$_surname; }
   public function getDOB()        { return $this->$_dob; }
   public function getAddress()    { return $this->$_address; }
   public function getTelephone()  { return $this->$_telephone; }
   public function getEmail()      { return $this->$_email; }
   public function getPassword()   { return $this->$_password; }

   public function setForename($value)       { $this->$_forename = $value; }
   public function setSurname($value)        { $this->$_surname = $value; }
   public function setDOB($value)            { $this->$_dob = $value; }
   public function setAddress($value)        { $this->$_address = $value; }
   public function setTelephone($value)      { $this->$_telephone = $value; }
   public function setEmail($value)          { $this->$_email = $value; }
   public function setPassword($value)       { $this->$_password = $value; }

   public function print_m() {
       echo getForename();
       echo getSurname();
       echo getDOB();
       echo getAddress();
       echo getTelephone();
       echo getEmail();
       echo getPassword();
   }

   public function hashPassword($password) {

       return password_hash($password, PASSWORD_BCRYPT);
   }

   public function selectAll()             { return 0; }
   public function selectSingle($value)    { return 0; }
   public function orderBy($value)         { return 0; }
   public function insertInto($value)      { return 0; }
   public function update($value)          { return 0; }
   public function delete($value)          { return 0; }
}
Reply
#2

It's the way PHP parses the script

PHP Code:
$myvalue 'variablename';
$class->variablename 'aaa';
echo 
$class->$myvalue// outputs 'aaa' 

You have to remove $ when you use $this->_forename.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB