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
- class Hello{
- public $out;
- private $text;
- public function __construct($text)
- {
- $this->text = $text;
- $this->sayHello();
- }
- private function sayHello()
- {
- $text = $this->text;
- $var = 'Hello, my name is: ' . $text;
- return $this->out = $var;
- }
- }
- $hello = new Hello('Tom');
- echo $hello->out;
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