Jump to content

I need help with some connection in OOP, newbie.


Ivan Ivković

Recommended Posts

It seems that a class can't inherit an object as attribute?

I'm trying to make my UpperNavigation class access the $this -> db attribute which is a mysqli connection.

Is this possible?

 

If not, how do I manage my connection in other classes?

 


class Connection{

public $state = false;
public $db;
public $db_table_names;

function __construct($name){

	include('dbconfig.php');

	$this -> db = new mysqli($dbconfig[$name]['server'], 
				$dbconfig[$name]['username'],
				$dbconfig[$name]['password'], 
				$dbconfig[$name]['database']);
	$this -> state = true;
	$this -> db_table_names = $db_table_names;
	$this -> db -> query("SET NAMES 'UTF8'");
}

function close(){
	if($this -> state == true){
		$this -> state = false;
		$this -> db -> close();
		unset($this -> db);
	}
}
}

 

This is my attempt of accessing Connection::$db connection.


class UpperNavigation extends Connection{

public $all_parents_sorted;

public function printUpperNavigation(){
	echo '<a href=\''.$this -> current_page.'\'>'.$this -> title . '</a>';
}

public function printSearchBar(){
	echo '<input type=\'text\' value=\'Search by name...\' 
		onfocus="if(this.value == \'Search by name...\'){this.value = \'\';}"
		onblur="if(this.value == \'\'){this.value = \'Search by name...\';}"/>';
}

public function fetchAllParents($sp_id){
	$latest_id = $sp_id;
	if(isset($this -> db)){
		echo 'yes';
	}else{
		echo 'no';
	}
	$query = 'SELECT default_name, type, sp_id FROM ' . 
	$this -> db_table_names['sport'] . ' WHERE sp_id = "' . $sp_id . '"'; 
	$result = $this -> db -> query($query); // THIS IS THE PROBLEM
}
}

Link to comment
Share on other sites

Ivan,

 

Your specific issue based on the code you provided, is that the connection class does not have a query method.  So of course you're getting an error.

 

This is a very good example of where dependency injection is (in my opinion) a superior solution.  Rather than try and explain it I'll just point you to this http://components.symfony-project.org/dependency-injection/documentation.  They do a fantastic job talking about why DI is a great approach, and also provide a DI Container class that is at the heart of the symfony 2 framework.  You don't have to use it, but it's there in case you like what you see.

 

The problem with your connection class as I see it, is that it is actually attempting to be a connection class, and a model class.  In other words, you have a mixture of database connection activity and setup, with specific data and tables.  I would recommend that you break those out into discrete classes.  Have a model class for any specific table.  You can pass an instance of your database connection object to the model class in the constructor.

 

Likewise your UpperNavigation class should not be trying to inherit from connection, as it has nothing to do with connections.  It is not a subtype of connection -- it s purpose is to facilitate the output of your menu from the looks of it.  That class should not have the internals of a specific database query -- simply pass in either a specific model, or array of model objects that make sense.  From the look of what you have there is only one table being used:  db_table_names['sport'].

 

Let's say that instead you had a SportModel.class.php class definition.  So someone might expect to find code like this:

 


$connection = new Connection($dbconfig);

$sportModel = new SportModel($connection);

$upperNav = new UpperNavigation($sportModel);

 

Obviously this is just a sketch, but I wanted to give you an idea of how you might simplify and decouple your classes.

 

 

 

 

Link to comment
Share on other sites

dzelenika :

 

Fatal error : Call to a member function query() on a non-object in sportvillecms\classes\page.php on line 152

 

gizmola:

 

It's not my class that has the query() method, but the object $db; that I'm trying to pass by using extend feature.

REMINDER:

$result = $this -> db -> query($query);

What You are saying would be: $result = $this -> query($query); // and that is not the case.

 

Thanks you for answering! Hope you help me further in time.

Link to comment
Share on other sites

dzelenika :

 

Fatal error : Call to a member function query() on a non-object in sportvillecms\classes\page.php on line 152

 

gizmola:

 

It's not my class that has the query() method, but the object $db; that I'm trying to pass by using extend feature.

REMINDER:

$result = $this -> db -> query($query);

What You are saying would be: $result = $this -> query($query); // and that is not the case.

 

Thanks you for answering! Hope you help me further in time.

 

Ok, I see the issue, I missed the mysqli call.

 

Yes, so the problem is that you have to explicity call the parent class constructor in your subclass:

 

class UpperNavigation extends Connection {

public function __construct() {
   parent::__construct();
}

 

This issue is described clearly in the php manual here:  http://us.php.net/manual/en/language.oop5.decon.php

 

    Note: Parent constructors are not called implicitly if the child class defines a constructor. In order to run a parent constructor, a call to parent::__construct() within the child constructor is required.

 

 

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.