killerprince182 Posted April 13, 2015 Share Posted April 13, 2015 I am trying to create a page which displays user profile. I have created two classes one is dbconnect(which is database class) and the other is Profile class(which shows profile). Now I am trying to pass object of dbconnect class as arguments for the constructor of profile class. I am getting error undefined variable database_profile Here is my code of profile php class Profile{ private $user_id; private $profile_array; private $database_profile; public function __construct($user,dbconnect $database_link){ $this->user_id = $user; $this->database_profile = $database_link; } public function make_array(){ $sql = "SELECT * FROM users WHERE id = '$this->user_id'"; $result = $database_profile->run_query($sql); $profile_array = mysqli_fetch_array($result, MYSQLI_ASSOC); echo $profile_array['first_name']; } public function show_profileinfo(){ } public function profilepic(){ return $profile_array['avatar']; } } //Loads database $database_connect = new dbconnect("localhost","root",""); $database_connect->select_database("selftest"); //Loads Profile $profile = new Profile($_SESSION['userid'],$database_connect); $profile->make_array(); Here is my code of dbconnect class class dbconnect{ public $database_selected; private $data_select; public function __Construct($host,$user,$pass){ $this->database_selected = mysqli_connect($host,$user,$pass); if (!$this->database_selected) echo "Connection to database failed"; else echo "Connection to database successful!"; } public function select_database($database_name){ $this->data_select = mysqli_select_db($this->database_selected, $database_name); if(!$this->data_select) echo "Connection to table failed"; else echo "Connection to table successful!"; } public function close_connection(){ $close = mysqli_close($this->database_selected); if ($close) echo "<p>Connection successfully closed</p>"; else echo "<p>Failure in closing connection</p>"; } //Returns aither true or false public function run_query($sql){ return $query_success = mysqli_query($this->database_selected,$sql); } } Quote Link to comment https://forums.phpfreaks.com/topic/295523-unable-to-pass-objects-as-arguments/ Share on other sites More sharing options...
maxxd Posted April 13, 2015 Share Posted April 13, 2015 (edited) In Profile::make_array(), you're calling $database_profile->run_query(), not $this->data_profile->run_query(). Edited April 13, 2015 by maxxd Quote Link to comment https://forums.phpfreaks.com/topic/295523-unable-to-pass-objects-as-arguments/#findComment-1508965 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.