I am making a simple PHP framework, this has a 'base' class, then includes other classes, like a database abstraction class and a form validation class.
I have got the classes all working and including, but I cant seam to reference another sub class from with in a sub class?
example:
PHP Code:
<?php
require_once('db.php'); // databse class require_once('validation.php'); // validation class
class Base {
function Base() {
// load extending classes $this->db = new Db(); $this->validation = new Validation();
}
function test() {
print_r($this->validation->run());
} }
class Db extends base {
function Db() {
}
function query($sql) { // } }
class Validation extends base {
function Validation() {
}
function run() {
$this->db->query("SELECT * FROM `table`");
} }
What I want to be able to do, is call a sub function from another sub function.
for example:
PHP Code:
<?php
// load main class, and my two sub classes $instance = new Base();
// fire a function which has access to my validation class, and my DB class. $instance->test();
however I get the PHP Error
Fatal error: Call to a member function query() on a non-object
I have tried using the parent:: tag at the top of my subclasses like below, but that didn't seam to help;
It sort of sounds to me like the Validation class is trying to call the query() method in Db. If so, Validation either needs to create its own Db object, or else you need to pass a reference to it as part of Validation's constructor.
"Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
~ Terry Pratchett in Nation
Bookmarks