473,395 Members | 1,488 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

OOP clarification

348 100+
Hey everyone. I'm back in search of a better understanding of OOP. I feel like these past couple of months have paid off for me because I am at a point where I am really beginning to understand how this all works. I'd like to get some clarification on a few things first.

My questions stem from wanting to use private members and methods. I know that there is a reason for making methods and members public and private and honestly I'm still not totally clear as to when to use one over the other. I have been refactoring some of old classes and in looking at them, I was mortified. I figured that when I was redoing them that I should try and stick with private members and methods where possible because that's a good thing I guess.

I had download an app a while back that used 5 classes. Each class was like 1500 lines or so and the code looked clean to me. I figured that was a good way to program. I started to do the same thing but after looking over some of my classes, I quickly noticed that my classes were nothing more than functions instead of objects.

In refactoring this code, I separated out the methods that required functionality inside the class and set my constructor to call a main method that would do what was needed and then return out the values I needed. I just want to make certain that I understand correctly before I continue any further.

If I have a class like so:

Expand|Select|Wrap|Line Numbers
  1. class Hello{
  2.     public  $out;
  3.     private $text;
  4.  
  5.     public function __construct($text)
  6.     {
  7.         $this->text = $text;
  8.         $this->sayHello();
  9.     }
  10.  
  11.     private function sayHello()
  12.     {
  13.         $text = $this->text;
  14.         $var = 'Hello, my name is: ' . $text;
  15.         return $this->out = $var;
  16.     }
  17. }
  18.  
  19. $hello = new Hello('Tom');
  20. echo $hello->out;
To me, this seems like the correct way to write a class. I am returning out the values that I want. The way I was doing it in the past was to call the method directly and didn’t seem right.

Does this look correct? I have a few more questions but I'll wait on those for now to see what the feedback is on this. I'll also show you how I was doing before.

Thank!

Frank
May 19 '09
51 3473
Dormilich
8,658 Expert Mod 8TB
Supposedly, the getters and setters reveal too much implementation detail because they are typed. What happens if the type changes in the future then everything that calls those getters and setters might have to be updated.
that's why there are interfaces for the classes, that prevents such problems.

on the other hand side, the calls for __get() and __set() are unlikely to change (though they don't work on public properties)
Jun 19 '09 #51
Atli
5,058 Expert 4TB
The thing about getters and setters...

Implementing a getter and setter on the same variable (effectively making it a public variable) might seem unnecessary, seeing as we have the public keyword, but simply making it public restricts how you can use it in the future.

Consider if, in the soon to become present, you have the need to run some code on the new value of a variable before it is set. If you had had the foresight to create a setter for it, this would be no problem. But if it is just a public variable, you can't do that without breaking every code that uses this variable.

As you have found out, a simple change to the class design can have a devastating effect on your entire application.

And moreover, if you intend to have some variables read-only and some public, having getters for some of them and direct calls to others will make your class horribly inconsistent. (I HATE inconsistencies :P)

In my mind it's either/or. Use getters/setters on everything or nothing.
(My preference being everything.)

Supposedly, the getters and setters reveal too much implementation detail because they are typed. What happens if the type changes in the future then everything that calls those getters and setters might have to be updated.
How is that any different from a public variable?
Would the code not have to be altered just as much if the type of a public member changed?
Not that this is an issue in PHP. It's a loosely-typed language so there is no need for typed methods/members.
Jun 19 '09 #52

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Wes | last post by:
I am trying to secure different files, mostly pdf, so only the person suppose to see the file that was designed for that individual can see it. I am using sessions to secure the actual web pages,...
4
by: Shea Martin | last post by:
Which of the following do I use delete instead of just delete. //1.) // not sure about this one, as char is of size 1 char *str = new char; //2.) //not sure about this one, as it is a...
3
by: John D. Sanders | last post by:
I have just upgraded MySQL from version 3.23 to version 4.1.8 and now I am getting the following error when I try to run a script that worked: install_driver(mysql) failed: Can't load...
2
by: Ethan | last post by:
This is a clarification of a previous message, which was not expressed very well. I have a set of checkboxes near the bottom of the page and a function that checks or unchecks all of them. But when...
9
by: Adam | last post by:
Hi, I am having problems having an include file rendered in my browser (IE6). The include file contains <A href> tags to be used as a navigation bar in the footer of a .html file. I am...
3
by: ma740988 | last post by:
Consider the 'C' source. void myDoorBellISR(starLinkDevice *slDevice, U32 doorBellVal) { doorBellDetected = doorBellVal; } void slRcv() { starLinkOpenStruct myOpenStruct;
3
by: solomon_13000 | last post by:
> Wonthaggi Civic Theatre 'WCT' Case Study > > The town of Wonthaggi has a theatre which is owned and > operated by the local council, it is called the > Wonthaggi Civic Theatre (WCT) and a wide...
0
by: chanchito_cojones | last post by:
Hi there, I was searching the net for some guidance in putting together a query that would select random records from the main table. I came across this and it works like a charm. SELECT TOP...
8
by: Sai Kit Tong | last post by:
In the article, the description for "Modiy DLL That Contains Consumers That Use Managed Code and DLL Exports or Managed Entry Points" suggests the creation of the class ManagedWrapper. If I...
2
by: ravir | last post by:
Hi, I am new to this group. I am working in Perl and shellscripts. I have a clarification regarding perl grep and pattern matching. I am writing a perl script to automate the process of code...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.