Jump to content

Unable to pass objects as arguments


killerprince182

Recommended Posts

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);
		}
	}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.