Page 1 of 1
scope of php objects...some advice
Posted: Fri Oct 24, 2008 5:36 am
by definewebsites
Hello there,
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..
Thanks
Re: scope of php objects...some advice
Posted: Fri Oct 24, 2008 6:29 am
by onion2k
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
Code: Select all
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.
Re: scope of php objects...some advice
Posted: Fri Oct 24, 2008 10:49 am
by Christopher
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.
onion2k wrote:Code: Select all
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.
Re: scope of php objects...some advice
Posted: Fri Oct 24, 2008 1:28 pm
by definewebsites
Thanks for the tips guys, I do appreciate. I will look into it again..
Re: scope of php objects...some advice
Posted: Fri Oct 24, 2008 7:25 pm
by josh
A class is not an object, think of a class as the blueprint of the object, you still have to build objects, which is called instantiating them
Re: scope of php objects...some advice
Posted: Mon Oct 27, 2008 10:55 am
by fredrik
Someone that don't know how to instantiate objects talking about building an MVC application? wtf has the world come to.
Re: scope of php objects...some advice
Posted: Mon Oct 27, 2008 11:09 am
by definewebsites
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.
Cheers
Re: scope of php objects...some advice
Posted: Mon Oct 27, 2008 10:54 pm
by josh
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
Re: scope of php objects...some advice
Posted: Tue Oct 28, 2008 8:30 am
by crazycoders
Not, logical error only, syntax errors make the compiler crash, this did not
