Do child class overriding meathods ned 2 b declared proteted

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
gautamz07
Forum Contributor
Posts: 331
Joined: Wed May 14, 2014 12:18 pm

Do child class overriding meathods ned 2 b declared proteted

Post by gautamz07 »

do child classes extending a parent class for example have a method of the parent class being over-ridden , does that class need to be declared protected or private ??

eg.

method in parent class:

Code: Select all

class person {
                protected function set_name($new_name) {
                        if ($new_name != "Jimmy Two Guns") {
                                $this->name = strtoupper($new_name);
                        }
                }
        } 
method override in child class :

Code: Select all

class employee extends person {
                protected function set_name($new_name) {
                        if ($new_name == "Stefan Sucks") {
                                $this->name = $new_name;
                        }
                }
        } 
is the protected necessary ???
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Do child class overriding meathods ned 2 b declared prot

Post by requinix »

It has to be the same as the parent's or more accessible, so protected or public. If you leave off the "protected" then it will be public.
User avatar
gautamz07
Forum Contributor
Posts: 331
Joined: Wed May 14, 2014 12:18 pm

Re: Do child class overriding meathods ned 2 b declared prot

Post by gautamz07 »

Thanks
Post Reply