Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.
I am building a web application using an MVC layout. I have all requests routed to my index page from which I parse the request and route to the appropriate controller/model/view.
I've realised that when I declare my objects in my index page such as:
By the time I get to my controller and try to use this variable/object $db, I get an error message saying something like "Trying to perform operation on non-object".
This has led me to declare my objects in each of my controllers as above which in many ways I feel is unnecessary repetition of code. I would appreciate any suggestions/advice as to why this could be happening..
An object deals with scope in the same way as a function ... something defined outside of the object isn't available inside the object unless it's passed to it. Personally, I define things like database connections in an include file, then pass them to objects as an argument in the constructor... eg
class myObject {
function __construct(&$db) {
$this->db = $db;
}
}
$db = new Database_Connection();
$object = new myObject($db);
The & before $db in the constructor argument list means it's a reference rather than a copy. That's not especially important most of the time but for some things, like logging for example, it's helpful if everything goes through the same object.
definewebsites wrote:I am building a web application using an MVC layout. I have all requests routed to my index page from which I parse the request and route to the appropriate controller/model/view.
My suspicion is that you have gotten ahead of yourself design-wise. MVC is not a "layout", though the Front/Action Controller architecture you are attempting may have a file/directory layout. Get the basics right in as simple a way as possible, and then apply these patterns as problems arise.
class myObject {
function __construct($db) {
$this->db = $db;
}
}
$db = new Database_Connection();
$object = new myObject($db);
The & before $db in the constructor argument list means it's a reference rather than a copy. That's not especially important most of the time but for some things, like logging for example, it's helpful if everything goes through the same object.
Definitely pass objects into other objects to make them available in that scope. Also, the ampersand is not necessary in PHP5 because object vars are all handles -- which are like references.
I actually do know how to instantiate an object. The folks who responded to my question did not quite understand what I was trying to explain, they assumed that I was talking about how to instantiate an object. I decided not comment on the misconception but to let it go and try to figure it out for myself.
It is clear that you also did not understand my question hence the comment you're making.
I get an error message saying something like "Trying to perform operation on non-object".
If you get that error it is because you're using an OOP operator on a variable that does not contain a reference to an object, could mean you didn't instantiate it could mean you typ-oed the variable name, its a syntax error though