Page 1 of 1
Small OOP problem
Posted: Mon Aug 25, 2008 8:42 am
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
Re: Small OOP problem
Posted: Mon Aug 25, 2008 8:50 am
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

Re: Small OOP problem
Posted: Mon Aug 25, 2008 9:10 am
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.
Re: Small OOP problem
Posted: Mon Aug 25, 2008 9:44 am
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.
Re: Small OOP problem
Posted: Mon Aug 25, 2008 9:55 am
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.
Re: Small OOP problem
Posted: Mon Aug 25, 2008 11:38 am
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();
?>
Re: Small OOP problem
Posted: Mon Aug 25, 2008 11:40 am
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.
Re: Small OOP problem
Posted: Mon Aug 25, 2008 11:56 am
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.