It is currently Fri Mar 29, 2024 12:03 am



Reply to topic  [ 3 posts ] 
objects, inheritance, and method/property types 
Author Message
Felix Rex
User avatar

Joined: Fri Mar 28, 2003 6:01 pm
Posts: 16646
Location: On a slope
Reply with quote
Post objects, inheritance, and method/property types
ok, for those unaware I've been crash coursing in C# recently and have been kneck deep in object oriented code lately. I've got a few questions for clarification...they're probably just OO, but C# has pushed them into my mind.

Basically, with asp.net with c#, using code-behind pages, there's a website-wide class in effect. Then every page has its own class that inherits the main, site class. I never did this in PHP, but in C# it's damned near required.

So, anyway, I noticed the following:

If I create a method or property (variable) in the site-wide class and call it private, it's not accessible via inheritance. If it's public or protected, it is. I guess that's by design.

Now...what's the best design strategy? For methods I'm kinda stuck...they either need to be protected, public, or I need to recreate them in every page-level class...the latter is retarded. So...protected or public? What's the difference? Does protected just not allow me to overload the method, or is there more to it? And of course, public I'd be able to call from outside and inherited class...can I do that with protected also?

How about variables? If I'm just sharing variables internally, should I make it protected, or public? Or should I make it private in the class and build a get/set method for the inheriting class to use? More importantly, why?

_________________
They who can give up essential liberty to obtain a little temporary safety, deserve neither liberty nor safety.


Fri Nov 03, 2006 4:35 pm
Profile WWW
Duke
User avatar

Joined: Mon Mar 31, 2003 8:59 am
Posts: 1358
Location: right behind you
Reply with quote
Post 
Properly designed, there should be no need for public properties. All properties should be private or protected, and accessed via public functions. This makes sure nothing accidentally fucks with your properties (which can be a pain to debug).

example:

class foo{
private $bar;
public function get_bar(){
return $this -> bar;
}
}

That might seem silly, but this allows you to do additional processing, as well as lock down your class so the only way in or out is to go through your methods. For example, you could have a method to get a variable from the database. You could get that variable on the fly using the method. Even if that property was static, having it abstracted with a method could allow you to change the database, table, or field and it would be transparent to the rest of your application. You don't ever want some other class to access properties directly.

Method should be public if they have to be called from outside the class, private if they are only called from within the class, and protected if they need to be inherited.

So for your example, all methods that need to be accessible by your program should be public. The methods in your parent class should be protected if they need to inherit. Otherwise they should be private. Again, properties should not be public. They should be private unless they need to inherit, in which case they would be protected.

A word of caution, some languages define protected differently. Some use protection only when you explicitly say a class extends another class. Others will allow classes in the same dir to access (fucked up, no?)


Fri Nov 03, 2006 9:28 pm
Profile YIM WWW
Felix Rex
User avatar

Joined: Fri Mar 28, 2003 6:01 pm
Posts: 16646
Location: On a slope
Reply with quote
Post 
heh, that's cool. I appreciate the response. Looks like I may need to do a bit of rebuilding. :p

_________________
They who can give up essential liberty to obtain a little temporary safety, deserve neither liberty nor safety.


Sat Nov 04, 2006 11:17 am
Profile WWW
Display posts from previous:  Sort by  
Reply to topic   [ 3 posts ] 

Who is online

Users browsing this forum: No registered users and 12 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by STSoftware.