Forum Topic: Php Oop

(192 views • 7 replies)

This topic is 1 page long.

<< < > >>
None

Loccie

Reply To Post Reply & Quote

Posted at: 4/22/09 06:19 AM

Loccie NEUTRAL LEVEL 16

Sign-Up: 02/27/04

Posts: 1,059

I've used OOP in other languages and just started learning it in php as well. What I like to know is if the following is possible and if it is how and if it's good practice.

$myGeneralObject->aSubObject->method();

So you have one masterclass and in that class you have different classes for each part of your website. So an object for your members, one for your pages, etc...


None

Jon-86

Reply To Post Reply & Quote

Posted at: 4/22/09 08:17 AM

Jon-86 NEUTRAL LEVEL 13

Sign-Up: 01/30/07

Posts: 3,946

I'm not sure if PHP supports multiple inheritance, but a quick google gave me this:
http://www.devshed.com/c/a/PHP/Object-In teraction-in-PHP-Introduction-to-Composi tion/

After a skim read it looks to be your answer, give that a try and let me know if its any help as I would be interested to find out but don't have the time to look into it myself.

PHP Main :: C++ Main :: Java Main :: irc.freenode.net

BBS Signature

None

henke37

Reply To Post Reply & Quote

Posted at: 4/22/09 09:17 AM

henke37 NEUTRAL LEVEL 23

Sign-Up: 09/10/04

Posts: 3,660

His code is legal and quite possible, he just needs to store an object reference in a property of another object.

Each time someone abuses hittest, God kills a kitten. Please, learn real collision testing.


None

DougyTheFreshmaker

Reply To Post Reply & Quote

Posted at: 4/22/09 10:02 AM

DougyTheFreshmaker NEUTRAL LEVEL 02

Sign-Up: 07/30/07

Posts: 519

As it relates to practice, the particular method of calling the function in the example given violates the Law of Demeter. From what I have read, many developers have mixed feelings on this particular rule, however. YMMV.

We should take care not to make the intellect our god; it has, of course, powerful muscles, but no personality.
Freshmaking
Brainscrape

BBS Signature

None

elbekko

Reply To Post Reply & Quote

Posted at: 4/22/09 10:26 AM

elbekko EVIL LEVEL 16

Sign-Up: 07/23/04

Posts: 6,587

As per your explanation, you probably wouldn't be using instances (or atleast not one of the super class), but just static methods.

"My software never has bugs. It just develops random features. " - Unknown

[ FluxBB developer | Quickmarks 0.5.1 | Strings & Ints - my blog ]

BBS Signature

None

VigilanteNighthawk

Reply To Post Reply & Quote

Posted at: 4/22/09 12:20 PM

VigilanteNighthawk LIGHT LEVEL 03

Sign-Up: 02/13/03

Posts: 1,686

At 4/22/09 06:19 AM, Loccie wrote: I've used OOP in other languages and just started learning it in php as well. What I like to know is if the following is possible and if it is how and if it's good practice.

$myGeneralObject->aSubObject->method();

Perfectly legal. In fact, this is how cakephp implements it's active record (of course, cake php manages to botch the living hell out of active record, making writing methods to act on instances of data damned hard.) As of php 5, you can also do this: $myObject->somemethod1()->somemethod2() so long as somemethod1 returns an object containing somemethod2.


So you have one masterclass and in that class you have different classes for each part of your website. So an object for your members, one for your pages, etc...

Bad idea. Registries (assuming this is what you are talking about), are usually a horrible idea. Every time you want to modify your program, you will almost always have to modify that class. The only time I think they ever remotely make any sense is for storing configuration values. If you want to try something like this, look into the Model View Controller and Front End Controller patterns. Most MVC based frameworks, like cakePHP and codeigniter implement them.

Also, you may want to check out SOLID practices. You can read up more about this here: http://www.oodesign.com/design-principle s.html If you haven't encountered this before, I highly recommend you look into it. I've found these practices to be extremely helpful. It was one of the programmers at newgrounds, (it was Dearon, I believe, but I can't be sure), that directed me to them, and I'm very grateful for it.

At 4/22/09 08:17 AM, Jon-86 wrote: I'm not sure if PHP supports multiple inheritance, but a quick google gave me this:
http://www.devshed.com/c/a/PHP/Object-In teraction-in-PHP-Introduction-to-Composi tion/

PHP does not support multiple inheritance, but it does support java style interfaces.


None

Jon-86

Reply To Post Reply & Quote

Posted at: 4/22/09 07:11 PM

Jon-86 NEUTRAL LEVEL 13

Sign-Up: 01/30/07

Posts: 3,946

At 4/22/09 12:20 PM, VigilanteNighthawk wrote: PHP does not support multiple inheritance, but it does support java style interfaces.

Thats good to know, now I will know what I'm looking for in a few months time :)

PHP Main :: C++ Main :: Java Main :: irc.freenode.net

BBS Signature

None

Loccie

Reply To Post Reply & Quote

Posted at: 4/24/09 04:22 AM

Loccie NEUTRAL LEVEL 16

Sign-Up: 02/27/04

Posts: 1,059

Thanks for the help. I'll use different classes instead of one container class with lots of methods in it. When I see a good use for the nested methods I'll certainly give it a try. Also thank you for the link with the OOP tips!


All times are Eastern Standard Time (GMT -5) | Current Time: 07:02 PM

<< Back

This topic is 1 page long.

<< < > >>
You need a Grounds Gold Account to post on the NG BBS! If you don't have one, click here to sign up now! It's fast, free, and easy — and opens up tons of great NG features!