class - PHP get value from function called by constructor -


i have feeling i'm overlooking simple can't figure out how value function called constructor within class. below simple example but, essentially, need use userid value on index.php page returned function getuser called constructor.

thank in advance help!

index.php:

$test = new test($username); //need value of userid here.... 

class/function:

class test {     //constructor     public function __construct($username)         {             $this->getuserid($username);         }      //get user id        public function getuserid($username)         {             //db query here id             return $userid;         } } 

i should add know can initialize class call function index.php , value way. simple example of scripts i'm working on call 6 or 7 functions within constructor perform various tasks.

you forgot return value of $this->getuserid($username); in constructor doesn't matter anyway in php constructors not return values. have make second call value after initiate object.

$test = new test(); $userid = $test->getuserid($username);  class test {     // constructor no longer needed in example      //get user id        public function getuserid($username)     {         //db query here id             return $userid;     } } 

or perhaps more intelligant:

$test = new test($username); $userid = $test->getuserid();  class test {     protected $username;      public function __construct($username)      {         $this->username = $username;     }      //get user id        public function getuserid()     {         // username access via $this->username          //db query here id             return $userid;     } } 

Comments

Popular posts from this blog

PHP DOM loadHTML() method unusual warning -

python - How to create jsonb index using GIN on SQLAlchemy? -

c# - TransactionScope not rolling back although no complete() is called -