OOP MVC

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
vilis
Forum Newbie
Posts: 3
Joined: Tue Dec 29, 2009 3:06 pm

OOP MVC

Post 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.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: OOP MVC

Post by AbraCadaver »

I'm not sure I understand. Are you extending a class? Some code would help.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
vilis
Forum Newbie
Posts: 3
Joined: Tue Dec 29, 2009 3:06 pm

Re: OOP MVC

Post 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.
User avatar
JNettles
Forum Contributor
Posts: 228
Joined: Mon Oct 05, 2009 4:09 pm

Re: OOP MVC

Post by JNettles »

I think he wants to do the singleton pattern. Either way, good link.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: OOP MVC

Post 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:

Code: Select all

Library2::test2();
Or if you needed the object then I guess in Library1 you could do this:

Code: Select all

$this->library2 = new Library2;
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
vilis
Forum Newbie
Posts: 3
Joined: Tue Dec 29, 2009 3:06 pm

Re: OOP MVC

Post 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;
}

}
MichaelR
Forum Contributor
Posts: 148
Joined: Sat Jan 03, 2009 3:27 pm

Re: OOP MVC

Post 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.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: OOP MVC

Post 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!
(#10850)
Post Reply