Jump to content

oop extends or implement


purencool

Recommended Posts

hi phpfreaks

 

I have an class that is called "connDatabase" what I want to do with it is either extend it or implement it.

But I don't know which one to use. The other issue I have is say class "a" has three vars that the that the construct

needs to obstantiate and "connDatabase" needs three of its own. How do I obstantiate? :shrug:

 

thanks for any help

Link to comment
Share on other sites

You cannot implement a class, only an interface, so that solves that.

 

I assume when you say obstantiate, you mean instantiate?

 

You can setup a __construct in a child class that calls the parent _-construct() before doing its own work.

 

class Foo extends Bar
{
  public function __construct()
  {
    parent::__construct();
    // do own setup
  }
}

 

On a side note. be careful extending a database classes. does the child really extend the database or just depend on it? There is a difference.

Link to comment
Share on other sites

sorry instantiate.

 

Below are the two classes I want to join. I have code that needs to call on information out

of the database. But  what I am trying to achieve is to extend the connDatabase onto the Controller i will

be able to query the database and not have to keep writing database connections.

 

I have a number of classes that needs this sort of functionality.

 

Note: if this is back practice please tell I would rather corrected now.

 

 

class connDatabase {
    private $dbConnection
    private $unlock;
    private $path;

    protected $host;
    protected $database;
    protected $username;
    protected $password;

    public function __construct ($dbConnection,$unlock,$path){
        $this->dbConnection = $dbConnection;
        $this->unlock = $unlock;
        $this->path = $path;
        $this->decryptLock();
    }
    private function decryptLock(){
        //decrypt

        //addhost and data


        //place into private
    }
    private function connection($query){

            $db = new PDO("mysql:host=$this->host;dbname=$this->database", "$this->username", "$this->password" );
            $db->beginTransaction();
            $db->exec ($query);
            $db->commit();
            $db->Null;
    }
    public function getConnection ($query){

        $this->connection($query);
    }

}

 

class controller {

    private $pages;
    private $language;
    private $accessLevel;
    private $path;
    private $defaultpage;

    public function __construct($default,$path, $pages, $language, $accessLevel) {
        $this->pages = $pages;//array showing pages
        $this->language = $language;
        $this->accessLevel = $accessLevel;
        $this->path = $path;
        $this->defaultpage =$default;
       
    }

    private function findTech($request) {



        return $return;
    }


    public function findTechReturn($request) {

        return $return;
    }

Link to comment
Share on other sites

I can tell just by the name 'controller' that it is not at all related to and therefore should not extend the database class.

 

The database class might be a dependency, but that's it. Even then, any database interactions should likely be moved into models (but I won't go into that).

 

You controller simply requires the Database class. This should be passed into your __construct() method.

Link to comment
Share on other sites

do you mean I can do something like this

 

$controller = new controller(controller vars ,$database = new connDatabase($var, $var, $var))

 

add the database connection as a var inside the class

class controller {

    private $pages;
    private $language;
    private $accessLevel;
    private $path;
    private $defaultpage;
    private $connectionDatabase;

    public function __construct($default,$path, $pages, $language, $accessLevel, $connectionDatabase) {
        $this->pages = $pages;//array showing pages
        $this->language = $language;
        $this->accessLevel = $accessLevel;
        $this->path = $path;
        $this->defaultpage =$default;
        $this->connection =$connectionDatabase;
       
    }

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.