Page 1 of 1
OOP MVC
Posted: Tue Dec 29, 2009 3:14 pm
by vilis
Hi.
I'm messing with OOP and cannot understand one thing.
If you have Core class, from which you call all other classes, how can the sub classes access each other.
I tried to make them accessible via $this, but it doesnt seem to work. Tried to make methods public but it didnt help.
Like in Subclass1 you call $this->subclass2->method_from_subclass2 and vice versa.
Subclasses in this case are classes called from Core.
Thanks.
Re: OOP MVC
Posted: Tue Dec 29, 2009 3:31 pm
by AbraCadaver
I'm not sure I understand. Are you extending a class? Some code would help.
Re: OOP MVC
Posted: Tue Dec 29, 2009 3:37 pm
by vilis
yes, I am extending.
class Core {}
class Library1 extends Core { function test1() {} }
class Library2 extends Core { function test2() {} }
For example, I want to call $this->library2->test2() from Library1 and so on.
Re: OOP MVC
Posted: Tue Dec 29, 2009 3:47 pm
by JNettles
I think he wants to do the
singleton pattern. Either way, good link.
Re: OOP MVC
Posted: Tue Dec 29, 2009 4:17 pm
by AbraCadaver
I think your design methodology is flawed. If you instantiate an object from class Library1 how can you use an object from class Library2 if it hasn't been instantiated yet? You can call statically:
Or if you needed the object then I guess in Library1 you could do this:
Re: OOP MVC
Posted: Tue Dec 29, 2009 5:15 pm
by vilis
Yes, Singleton Pattern looks similar to what I tried to make.
Can anyone tell what are cons and pros of using Singleton for MVC framework ?
AbraCadaver, I thought it could be possible because I already called both classes in the Core class. Oh, I missed to say that before.
class Core {
function __construct(){
$this->lib1 = new Library1;
$this->lib2 = new Library2;
}
}
Re: OOP MVC
Posted: Tue Dec 29, 2009 5:20 pm
by MichaelR
vilis wrote:I thought it could be possible because I already called both classes in the Core class. Oh, I missed to say that before.
Imagine the main class "Core" to be called "Dog". Imagine Library 1 to be "Alsatian" and Library 2 to be "Labrador". You can't make an Alsation do Labrador-specific things, or a Labrador do Alsation-specific things. They can only do their own specific things and general Dog things.
Re: OOP MVC
Posted: Wed Dec 30, 2009 2:57 am
by Christopher
vilis wrote:Can anyone tell what are cons and pros of using Singleton for MVC framework ?
If you search, you can find tons of information about using the Singleton. As for in a "MVC framework", I noticed that Zend is removing most of the Singletons for its 2.0 release. There is probably a discussion of that decision somewhere.
PS - "OOP MVC" has to be the ultimate buzzcronym post title for a PHP forum! Well done!