Best practice to show user data in PHP -
this user
class getting user data.
require_once "classes/db.php"; class user { private $session_user_id = 1; //$session_user_id = $_session["user_id"]; private $user_data; public $username; public $first_name; public $last_name; public $profile_photo; private static $instance = null; //kthen objekt. private function __construct() { $db = db::get_instance(); $query = "select * `users` `user_id` = $this->session_user_id"; $run_query = $db->mysqli->query($query); $this->user_data = $run_query->fetch_assoc(); $this->username = $this->user_data["username"]; $this->first_name = $this->user_data["first_name"]; $this->last_name = $this->user_data["last_name"]; $this->profile_photo = $this->user_data["profile"]; } public static function get_instance() { if(!isset(self::$instance)) self::$instance = new self; return self::$instance; } public function first_name() { return $this->first_name; } } $user = user::get_instance();
can tell me best method make private properties
, content using public get_name();
method echo $user->get_last_name()
?
or echo property content? echo $user->last_name
.
extra question: how can use $_session
variables inside class? should pass function parameter?
firstly, should view popular framework designs $_session
question.
in opinion, it's better use getter , setter methods object-oriented approach. because these variables cannot modified outside when compared direct call of variables class. also, can use protected variables better privates.
you can read these topics stack overflow:
Comments
Post a Comment