barraclm
-
Posts
2 -
Joined
-
Last visited
Posts posted by barraclm
-
-
My script has 3 classes (that are relevant to this discussion): DB, User and Validate. They are all in independent files and loaded automatically, when required, by an autoloader.
Both the User class and the Validate class have a private variable $_db which is instantiated by their class constructor: $this->_db = DB::getInstance();
At the top of the script I declare new instances: $user = new User(); and $validate = new Validate();
I then call a method in $user: $found = $user->findInUsers ( .... )
From within the findInUsers method I then want to call the query method in the DB class. My problem is that I can't find a way to do this that works.
I have tried $this->_db->query( ... ), $_db->query( ... ), but neither works.The error messages I am getting are:
An error occurred in script '/srv/http/classes/User.php' on line 42: Undefined variable $_db
Uncaught Error: Call to a member function query() on null in /srv/http/classes/User.php:42Any pointers as to what I am doing wrong, or what I should be doing, would be most welcome.
Problem with calling a method from within a method in another class
in PHP Coding Help
Posted
Thanks to everyone who replied. I am now sorted. I have brought better clarity to the structure of my application and that has helped me see some problems that I was causing myself.