Now, let’s go back and further consider those variables we defined in the class MyClass from the previous code sample, $a and $b. When defining members within a class, it’s possible to define some additional attributes for each member, by way of the public and private keywords, placed before the definition of each member to state whether we want them to be publically accessible, or private. This enforces some interesting behaviour.
The keywords public and private define how we wish a method or member to be accessed from outside the class, within the global scope of the PHP script. by specifying a method or member to be private, we are declaring that item to be restricted for use within the class only
Here is an example
Class MyClass { public $a = 5; private $_b = 10; function getB(){ echo $this->_b; } function _getB(){ return $this->_b; } } $class = new MyClass; echo $class->a; echo $class->_getB(); echo $class->_b; echo $class->getB();
5 Fatal error: Call to private method MyClass::_getB() from context '' on line 17 Fatal error: Cannot access private property MyClass::$_b on line 18 10
(Here, error reporting has been enabled, and the two seperate error messages are shown even though execution would normally halt after a Fatal error)
Let’s look a little closer at what’s happening here. We have defined a public member $a, and a private member $_b. We also have a private method _getB() and a public method getB() - note the underscore preceding the private method. Private methods and members traditionally start with an underscore in their name, wheras public methods and members don’t. This naming convention isn’t required, but helps to identify these private items.
The first echo statement outside the class outputs the value of $a from within the class instance. Because we’ve defined the member $a to be public we can access its value here, and proceed as normal.
But, because the method _getB() has been declared private, we cannot access it from outside the class, the way we did before. We recieve the first error in this case, informing us that the method is private, and we cannot access it from the global context
Similarly, because the member $_b is also private, we cannot directly access the value of that either, prompting the second error.
But, the method getB() is defined as public and as with the member $a we can access this method from outside the class. We can use this public method to access the private, hidden members and methods within MyClass, since any methods within a class have access to all of that classes private members and methods. Here, getB() has access to the private member $_b and so can safely return the value of $_b back to the global scope of the code, where we print it to screen.
Defining members and methods as private or public is not vital to ensure the correct operation of Object Oriented code, but they do help to compartmentalise your scripts, and allow you to control what data is passed in and out of each object, effectively allowing you to limit how much of each object you expose to the outside world, treating them like “black boxes”.
Here is an example which demonstrates how this behaviour can be useful in a real-world coding example
Class ScoreCalculator { private $_var_x = 4.16; private $_var_y = 7.33; public getScore($score) { return ($score*$this->_var_x)+$this->_var_y; } } $calc = new ScoreCalculator(); echo "The score is ".$calc->getScore(25);
The score is 111.33
This is a pretty arbitrary piece of code, which generates a “score” from a special algorithm given a particular numerical input. Consider a scenario where this piece of code has been written upon request from another programmer, who wishes to have a function which generates a score based on some arbitrary values. This other programmer doesn’t care what the algorithm is that calculates the score - that’s not in his job description. All he wants is a package, with a public function getScore() which will generate the value for him
Using private methods and members, you could then write the above class to send to the programmer, safe in the knowledge that you can safely modify the code in future and even change the variable names of $var_x and $var_y, if required, without affecting any of the other programmer’s code or causing runtime errors. This is because there is no way he can access these variables directly from outside the class.
This feature is a major benefit when using classes, especially in code that will be examined and modified by several other people




