oop - How can I assign a value to public variable from inside static function in php? -


i tried $this-> not assign value $first_name , $last_name variable. without removing static feature of function , without inserting static feature variables, how can echo full_name()? here code :

<?php  class user  {      public $first_name;     public $last_name;    public function full_name() {     if(isset($this->first_name) && isset($this->last_name)) {     return $this->first_name . " " . $this->last_name;     } else {     return "no name!";     }    }    public static function verify() {      $this->first_name = "firstname";     $this->last_name =  "last_name";    }   }  $user = new user(); user::verify(); echo $user->full_name()  ?> 

you can't really... other using singleton pattern, rather defeats point of static function.

the basic idea static function doesn't require instance, you're trying does.
i'd suggest verifying data in non-static setter function, or in constructor. or, if needs must, add public method verify isn't static.
think it: statics don't need instance, verifying, if there no instance (and therefore no "$this").


Comments

Popular posts from this blog

jquery - Invalid Assignment Left-Hand Side -

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -