Small OOP problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
SweetieCakes
Forum Newbie
Posts: 3
Joined: Mon Aug 25, 2008 8:35 am

Small OOP problem

Post by SweetieCakes »

Hi

I have a slight OOP problem.

Code: Select all

<?php
 
include("adodb/adodb.inc.php");
 
class BaseClass {
    
    protected $db;
    protected $testclass;
    
    function __construct() {
        
        $this->db = ADONewConnection("mysqli");
        $this->db->Connect("localhost","root","","test");
        
        $this->testclass = new ChildClass();
    
    }
    
    function getTestTable() {
        return $this->testclass->getTable();
    }
    
}
 
class ChildClass extends BaseClass {
    
    function __construct() {
        
    }
    
    function getTable() {
        return $this->db->GetAll("SELECT * FROM test");
    }
    
}
 
$test = new BaseClass();
$data = $test->getTestTable();
 
?>
This doesn't work. It returns the following error:

Fatal error: Call to a member function GetAll() on a non-object...

What to do? Thank you in advance
User avatar
jaoudestudios
DevNet Resident
Posts: 1483
Joined: Wed Jun 18, 2008 8:32 am
Location: Surrey

Re: Small OOP problem

Post by jaoudestudios »

Why extend the first class, why not put it all in one,

But if you want to extend it, I had a quick look at your code and I think you should try the following...
To access 'db' object from the extended/child class, you can not use 'this', use the parent class name.
i.e. BaseClass->db->....

I hope that is correct, if not, I am sure another developer will show us both the way :)
SweetieCakes
Forum Newbie
Posts: 3
Joined: Mon Aug 25, 2008 8:35 am

Re: Small OOP problem

Post by SweetieCakes »

jaoudestudios wrote:Why extend the first class, why not put it all in one,

But if you want to extend it, I had a quick look at your code and I think you should try the following...
To access 'db' object from the extended/child class, you can not use 'this', use the parent class name.
i.e. BaseClass->db->....
Hi. Your suggestion doesn't work (BaseClass-> is invalid syntax). The code I pasted was just an example of the problem with a larger system that needs to be in multiple classes.

Thank you anyways.
User avatar
Ziq
Forum Contributor
Posts: 194
Joined: Mon Aug 25, 2008 12:43 am
Location: Russia, Voronezh

Re: Small OOP problem

Post by Ziq »

In ChildClass variable $db and $testclass empty. You must transfer $db variable in ChildClass to use it.

For example:

Code: Select all

 
<?php
 
include("adodb/adodb.inc.php");
 
class BaseClass {
   
    protected $db;
    protected $testclass;
   
    function __construct() {
       
        $this->db = ADONewConnection("mysqli");
        $this->db->Connect("localhost","root","","test");
       
        $this->testclass = new ChildClass($db);
   
    }
   
    function getTestTable() {
        return $this->testclass->getTable();
    }
   
}
 
class ChildClass extends BaseClass {
   
    function __construct($db) {
       $this->db = $db;
    }
   
    function getTable() {
        return $this->db->GetAll("SELECT * FROM test");
    }
   
}
 
$test = new BaseClass();
$data = $test->getTestTable();
 
?>
 
Look at 16, 28, 29 lines.
SweetieCakes
Forum Newbie
Posts: 3
Joined: Mon Aug 25, 2008 8:35 am

Re: Small OOP problem

Post by SweetieCakes »

Hello

Thank you for your help. Would there be an efficient solution because I have a lot of these variables I must pass.
User avatar
panic!
Forum Regular
Posts: 516
Joined: Mon Jul 31, 2006 7:59 am
Location: Brighton, UK

Re: Small OOP problem

Post by panic! »

this is very simple:

the BaseClass method 'getTestTable' calls the function 'getTable' which is a method of 'ChildClass'. Which isn't implemented in your BaseClass.

if you run

Code: Select all

 
$test = new ChildClass();
$data = $test->getTestTable();
 
your code will run BUT this doesn't solve the fact that BaseClass may call a method that doesn't exists.

to prevent this from happening declare BaseClass as an abstract class and getTable as an abstract method.

http://uk.php.net/abstract

Which means BaseClass on it's own cannot be declared, but only other functions that inherit it using the extends keyword.

declaring getTable as an abstract function within BaseClass means any class that extends it must implement a getTable function.

This should work, but I haven't tested it. Let me know if you need any further help.

Code: Select all

 
<?php
 
include("adodb/adodb.inc.php");
 
abstract class BaseClass {
   
    protected $db;
    protected $testclass;
   
    function __construct() {
       
        $this->db = ADONewConnection("mysqli");
        $this->db->Connect("localhost","root","","test");
       
        $this->testclass = new ChildClass();
   
    }
   
    function getTestTable() {
        return $this->testclass->getTable();
    }
     abstract protected function getTable();
}
 
class ChildClass extends BaseClass {
   
    function __construct() {
       
    }
   
    function getTable() {
        return $this->db->GetAll("SELECT * FROM test");
    }
   
}
 
$test = new ChildClass();
$data = $test->getTestTable();
 
?>
 
User avatar
panic!
Forum Regular
Posts: 516
Joined: Mon Jul 31, 2006 7:59 am
Location: Brighton, UK

Re: Small OOP problem

Post by panic! »

Ziq wrote:In ChildClass variable $db and $testclass empty. You must transfer $db variable in ChildClass to use it.
I don't think that is right, the inheriting class will inherit all the variables and methods of the inherited class.
User avatar
Zoxive
Forum Regular
Posts: 974
Joined: Fri Apr 01, 2005 4:37 pm
Location: Bay City, Michigan

Re: Small OOP problem

Post by Zoxive »

SweetieCakes wrote:Hi

I have a slight OOP problem.

Code: Select all

<?php
 
include("adodb/adodb.inc.php");
 
class BaseClass {
    
    protected $db;
    protected $testclass;
    
    function __construct() {
        
        $this->db = ADONewConnection("mysqli");
        $this->db->Connect("localhost","root","","test");
        
        $this->testclass = new ChildClass();
    
    }
    
    function getTestTable() {
        return $this->testclass->getTable();
    }
    
}
 
class ChildClass extends BaseClass {
    
    function __construct() {
        
    }
    
    function getTable() {
        return $this->db->GetAll("SELECT * FROM test");
    }
    
}
 
$test = new BaseClass();
$data = $test->getTestTable();
 
?>
This doesn't work. It returns the following error:

Fatal error: Call to a member function GetAll() on a non-object...

What to do? Thank you in advance
I'm not sure whats with all the other posts...

But your doing one thing wrong.. not running the __construct off the parent class. (see code comment)

Code: Select all

<?php
 
include("adodb/adodb.inc.php");
 
class BaseClass {
    
    protected $db;
    //protected $testclass;
    
    function __construct() {
        
        $this->db = ADONewConnection("mysqli");
        $this->db->Connect("localhost","root","","test");
        
        //$this->testclass = new ChildClass(); // nesting a class that extends this one? what?
    
    }
    /*
    function getTestTable() {
        return $this->testclass->getTable();
    }
    */
}
 
class ChildClass extends BaseClass {
    
    function __construct() {
       parent::__construct(); // run parent construct function OR simply remove this function declaration if you are not doing anything extra here.
    }
    
    function getTable() {
        return $this->db->GetAll("SELECT * FROM test"); // $this->db didnt exist before because you never called the parent::__construct
    }
    
}
 
$test = new ChildClass();
$data = $test->getTable();
 
?>
However you seem to be nesting classes that extend each other.. I think you may have some confusion here.
Post Reply