0

I have 2 classes (2 files)

db.class.php
user.class.php

I want to use some functions from db.class.php:

db.class.php


Class DBManager {

      /** all functions goes here ...*/

}
$DB = new DBManager();

The content of user.class.php is:



Class User extends DBManager {
    function User() {

    }

    function Total($table) {
      $query = $DB->Execute("SELECT * FROM $table");
      $total = $DB->NumRows($query);
      return $total;
    }


}

$User = new User();

When I want to use my new function total($table) I get 2 errors:

Undefined variable: DB in ..\class\user.class.php on line 14


Fatal error: Call to a member function Execute() on a non-object in ..\class\user.class.php on line 14

I includes the 2 classes in my main.php file like:



include 'class/db.class.php';
include 'class/user.class.php';


Edit 1:

Related post: best trick when using an extending class (PHP)

7
  • (reference) Classes and Objects - Basics Commented Jan 29, 2011 at 10:49
  • Any reason why you are not using PHP5 syntax? If you are following a tutorial there, you should get a newer one. Commented Jan 29, 2011 at 10:51
  • @Gordon: I already have my classes. I have an issue here. for what this reference? Commented Jan 29, 2011 at 10:52
  • 1
    @Mini Because apparently you're missing some basics about classes… :o) Commented Jan 29, 2011 at 10:54
  • @Gordon: How can I rewrite it in PHP5? @Deceze: How? Thank you anyway :) Commented Jan 29, 2011 at 10:56

5 Answers 5

1

I suppose you want to call the parent function so try somenthing like that

 function Total($table) {
      $query = parent::Execute("SELECT * FROM $table");
      $total = parent::NumRows($query);
      return $total;
    }

And if it doesnt't work try with this instead of parent

PHP parent codumentation

Sign up to request clarification or add additional context in comments.

1 Comment

Yes I should use parent function. Thanks
1

Well, yes, $DB is not defined/in scope within the Total function.
If $DB is a property of the class, you'll need to access it with $this->DB.


Update after update:

You're instantiating the DBManager into the global variable $DB. This variable is only in scope in the global scope. It is not automatically available in any other scope, like inside functions. Whether or not User inherits from DBManager is completely irrelevant, the instantiated variable $DB is not in scope inside any function.

There's a lazy way to access variables in the global scope, but I'm not going to mention that here. The proper way would be to instantiate $DB as a class member of User or pass it upon instantiating User:

class User {

    protected $DB = null;

    public function __construct($DB) {
        $this->DB = $DB;
    }

    public function Total() {
        $this->DB->…
    }
}

$DB = new DBManager();
$user = new User($DB);

2 Comments

in the db.class.php I create $DB $DB = new DBManager();
@Mini Are you assigning it as a property to $this anywhere? If not, it goes out of scope as soon as the functions returns.
0

What is that $DB? You never set that to anything, so even if you include those files, you cannot reach that variable

Depending on your db.class.php, you probably need to instantiate some sort of $DB like this

$DB = new db();

(either in the function itself, or in a constructor for the user class using $this->DB)

try something like this:

Class User extends DBManager {
    private $DB;

    function User() { // should be __construct() really
        $this->DB = new DBManager();
        $this->DB->Connect();
    }

    function Total($table) {
      $query = $this->DB->Execute("SELECT * FROM $table");
      $total = $this->DB->NumRows($query);
      return $total;
    }


}

$User = new User();

1 Comment

But you can't reach that $DB inside the memberfucntion Total. I'll update my answer
0

Looks like you're confused about how inheritance works. The sub-class inherits the public and protected members and methods of the parent class. Therefore, in the example you've given, you should probably be using $this->Execute("SELECT * FROM $table") and $this->NumRows($query). Although the Execute and NumRows functions aren't defined in the User class, they are in the DBManager class, so you access them as if they were defined in User.

Comments

0

The solution for my issue is:


$query = DBManager::Execute("SELECT * FROM $table");
$total = DBManager::NumRows($query);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.