Jump to content

Recommended Posts

I have a user class that is very dependent on the database class, which is why the user class extends the database. I tried creating a protected method in the parent class called getDBCObject, which returned the database object/variable/handle that I want the user to have access to. I tried the method below, but it doesn't work:

 

<?php

/*
* @DATABASE
* ~~~~~~~~~~~~
* @FILE DESCRIPTION: Handles all database related processes
* @LAST MODIFIED: April 4, 2012
*/

class database
{
    protected $dbc;
    
    function __construct($db_host, $db_name, $db_user, $db_password)
    {
        try
        {
            $this->dbc = new PDO("mysql:host=$db_host;dbname=$db_name", $db_user, $db_password);
        }
        catch(PDOException $e)
        {
            echo '<b>An error occured while trying to create a database connection: </b>'. $e->getMessage();
        }
    }
    
    /*
     * @METHOD  getDBCObject
     * @DESC    Gives the $dbc object/variable to its child classes
     */
    
    protected function getDBCObject()
    {
        return $this->dbc;
    }
}

?>

 

My user class:

 

<?php

/*
* @DATABASE
* ~~~~~~~~~~~~
* @FILE DESCRIPTION: User related proccess
* @LAST MODIFIED: April 5, 2012
*/

class user extends database
{
    protected $dbc;
    
    public function __construct()
    {
        if(parent::getDBCObject() == null)
        {
            echo '<br/>A database class/connection is required before creating the user class.';
        }
    }
    
    public function isLoggedIn()
    {
        if($_COOKIE['user'])
        {
            //soon to come
        }
        else
        {
            return false;
        }
    }
}

?>

 

Any feedback on how I can let the user class use the $dbc variable in the database class?

Wrong syntax. Use $this-> like you would for most everything else.

 

But

I have a user class that is very dependent on the database class, which is why the user class extends the database.

No. That's not what inheritance is about. The only time you would do this is if the user was a type of database. Which it isn't.

If the user class needs access to database stuff then give it access to database stuff. Don't try to force an inheritance hierarchy that doesn't make sense.

A *user* is not a *type* of database so the relationship makes no sense. A user might depend on a database, but that is as far as the relationship should stretch.

 

As for your issue:

 

Firstly, you reference methods and properties declared within a parent class using $this not parent. Just as you would any local method or property. The only time you need to use *parent* is when you are overriding a parents method and within that child method you need to call the original parent method.

 

Secondly, you have not implemented the child __construct in such a mannor that you will be able to create a database object.

 

These two points are mute however as like I already stated, user should not be extending database.

Hmm, I understand what you're saying. But do you have any suggestions how I can allow the user access class access to the database without creating two different instances of the database class? Such as on index.php, it creates a database object - how would the class also work with that object/instance of the database class without creating another one of its own?

 

I hope I've worded that in an understandable manner. :/

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.