I'm just starting out OOP in PHP and using a tutorial I found here... I'm having a bit of trouble with the inheritance bit as when I run the code I get the following
Fatal error: Call to protected method person::change_name() from context '' in C:\Program Files (x86)\EasyPHP-5.3.6.1\www\index.php on line 77
In the following code I was under the impression protected access modifiers allowed the parent and child classes to access methods, so I'm not sure why the new object stefan of the parent type is unable to access the change_name() method.
<?php
/*classlib.php for the object code...
a class is an object. its varibales called attributes/properties and
its functions called its methods. methods are used to manipulate its
own attributes/properties
*/
class person{
var $name;
/*accessible outside the object..var keyword also public*/
public $height;
/*accessible by inherited classes*/
protected $sin;
/*accessible only to the object*/
private $pin;
//constructor function. called on the objects instantiation
function __construct($persons_name){
$this->name= $persons_name;
}
function get_name(){
return $this->name;
}
//can be used by child class..
protected function change_name($new_name){
if (name !="Jimmy Two Guns"){
$this->name=strtoupper($new_name);
}
}
/*fucntions can have access modifiers too..
thus this can not be called outside this object*/
private function get_pin(){
return $this->pin;
}
}
/*using 'extends' to utilize inheritance*/
class employee extends person{
function __construct($employee_name){
//notice we have access to person->set_name()
//because employee is just an extension of person
$this->change_name($employee_name);
}
//we overide method from parent class by naming it the same
protected function change_name($new_name){
if ($new_name == "Stefan Sucks"){
$this->name = $new_name;
}
//using object::method() to access an objects method
//using :: allows you to speficfy the class you want to
//search for a method or could have used parent::method()
else if ($new_name == "Johnny Fingers"){
person::change_name($new_name);
}
}
}
?>
<html>
<head>
<title>kILLER pHP cLASS TUTORIAL</title>
</head>
<body>
<?php
/*instantiate an object of person type*/
$stefan= new person("Harry Potter");
echo "stefan's name is: ". $stefan->get_name();
echo '<br>';
/*now we work on the object using its methods*/
$stefan->change_name("Stefan Mischook");
/*access the objects properties we set for them*/
echo "using the object's method: ". $stefan->get_name()."<br>";
//directly accessing properties in a class is a no no
echo "accessing the properties directly: ". $stefan->name. "<br>";
//don't do this. the idea behind OOP is to abstract and encapsulate
//data. an object's properites are its own to be handled via its
//methods
//becasue get_pin() is private to the function only the obj itself
//can call it. ergo it can only be used INSIDE the object
//$stefan->get_pin();
/*using inheritance in our objects*/
$james= new employee("Johnny Fingers");
echo "---> ". $james->get_name();
?>
</body>
</html>