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>

    You have declared the change_name() method to be "protected", but only "public" methods can be accessed directly from outside of the object. ("Protected" and "Private" methods can only be accessed from within the object itself, normally via the special $this variable.)

      And its as simple as that..lol. thanks NogDog, I guess I should just create a different function to change the name then and make that public. Much appreciated. I see my error now.

        Please use php tags instead of code tags around php code.

        c/tcp/ip;10987329 wrote:

        protected access modifiers allowed the parent and child classes to access methods

        Correct

        c/tcp/ip;10987329 wrote:

        a class is an object.

        No, a class is a generic definition for some set of objects: what properties and what methods exist for this set. An object is a particular instance of a class.

        c/tcp/ip;10987329 wrote:

        methods are used to manipulate its own attributes/properties

        Usually, but not necessarily

        c/tcp/ip;10987329 wrote:
        class person{
        	var $name;
        

        Don't use the "var" keyword. It's a remains from PHP 4 when there was no visibility modifiers, and means the variable will have public visibility.

        c/tcp/ip;10987329 wrote:

        // directly accessing properties in a class is a no no
        echo "accessing the properties directly: ". $stefan->name. "<br>";

        I'd rather claim that definining properties as public is a no no, but if they are public, directly accessing them is not. There might be bounds checking etc performed before a new value is assigned when you access a setter function for a variable, but if there isn't, it doesn't matter if you access it one way or the other.
        Also, if you can't trust the class definition to set property visibility to protected / private as needed, then you should probably think twice about using that class.

        All of this usually means that variables should be defined as private or protected. The setter or getter functions for each particular variable should be public if the outside world is supposed to be able to modify and / or access a particular variable.

          thank you very much johanfm for taking the time to set me straight on those issues. Including the php code tag thingy. It was irritating me that 'frame' being used with the code tags.

          Thanks again to you all.

            Write a Reply...