Cloud Stack Ninja

This is probably a basic question but im following this tutorial and at one point the code looks something like this.

<?php

class person
{
    public $name;
    public $height;
    protected $social_security_no;
    private $pin_number = 3242;

    public function __construct($person_name)
    {
        $this->name = $person_name;
    }
    public function set_name($new_name)
    {
        $this->name = $new_name;
    }

    protected function get_name()
    {
        return $this->name;
    }

    public function get_pin_number_public()
    {
        $this->pub_pin = $this->get_pin_number();
        return $this->pub_pin;
    }

    private function get_pin_number()
    {
        return $this->pin_number;
    }

}

class employee extends person
{

    public function __construct($person_name)
    {
        $this->name = $person_name;
    }

    protected function get_name()
    {
        return $this->name;
    }
}

However when i use this

<?php include "class_lib.php";?>
    </head>
    <body id="theBody">
    <div>

<?php
$maria = new person("Default");

$dave = new employee("David Knowler");
echo $dave->get_name();
?>

i get this error

Fatal error: Call to protected method employee::get_name() from context '' in C:\Users\danny\Documents\Workspace\test\index.php on line 13

The problem seems to be when i add protected to the get_name() function in the employee class but it seems to me that this is the preferred way to override in the tutorial. Any ideas?



Read more here: https://stackoverflow.com/questions/19276734/why-cant-i-call-this-protected-function-in-php

Content Attribution

This content was originally published by lorless at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: