Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP protected classes and properties, protected from whom?

Tags:

oop

php

protected

I'm just getting started with OOP PHP with PHP Object-Oriented Solutions by David Powers, and am a little curious about the notion of protection in OOP.

The author clearly explains how protection works, but the bit about not wanting others to be able to change properties falls a bit flat. I'm having a hard time imagining a situation where it is ever possible to prevent others from altering your classes, since they could just open up your class.php and manually tweak whatever they pleased seeing as how PHP is always in plain text.

Caution: all of the above written by a beginner with a beginner's understanding of programming.

like image 316
Drew Avatar asked Jan 10 '11 02:01

Drew


People also ask

What is protected in PHP class?

protected - the property or method can be accessed within the class and by classes derived from that class. private - the property or method can ONLY be accessed within the class.

How do I access protected members in PHP?

We can use bind() or bindTo methods of Closure class to access private/protected data of some class, for example: class MyClass { protected $variable = 'I am protected variable!

What is class properties PHP?

Data members declared inside class are called properties. Property is sometimes referred to as attribute or field. In PHP, a property is qualified by one of the access specifier keywords, public, private or protected. Name of property could be any valid label in PHP.

Can I override protected method in PHP?

The problem isn't that you cannot override the protected method, it's that you are calling a protected method from outside of the class. After the class is instantiated, you can call a public method which in turn could call get_name() and you will see that the code will work as expected.


3 Answers

From yourself!

You use various levels of protection to indicate how you want a class to be used. If a class member is protected or private, it can only be accessed by the class itself. There's no chance you can screw up the value of that member accidentally from "external" code (code outside the class).

Say you have a class member that is only supposed to contain numbers. You make it protected and add a setter which checks that its value can only be numeric:

class Foo {

    protected $num = 0;

    public function setNum($num) {
        if (!is_int($num)) {
            throw new Exception('Not a number!!!');
        }
        $this->num = $num;
    }
}

Now you can be sure that Foo::$num will always contain a number when you want to work with it. You can skip a lot of extra error checking code whenever you want to use it. Any time you try to assign anything but a number to it, you'll get a very loud error message, which makes it very easy to find bugs.

It's a restriction you put on yourself to ease your own work. Because programmers make mistakes. Especially dynamically typed languages like PHP let you silently make a lot of mistakes without you noticing, which turn into very hard to debug, very serious errors later on.

By its very nature, software is very soft and easily degrades into an unmaintainable Rube Goldberg logic machine. OOP, encapsulation, visibility modifiers, type hinting etc are tools PHP gives you to make your code "harder", to express your intent of what you want certain pieces of your code to be and enable PHP to enforce this intent for you.

like image 83
deceze Avatar answered Oct 10 '22 03:10

deceze


Protected is not really protecting from anyone to change the source code, but is just a class method visibility in PHP OOP

Class members declared public can be accessed everywhere. Members declared protected can be accessed only within the class itself and by inherited and parent classes. Members declared as private may only be accessed by the class that defines the member.

like image 43
ajreal Avatar answered Oct 10 '22 04:10

ajreal


They mean they are protected in different ways...

  • Private variables are not visible to anywhere except from within the class.
  • Protected variables are not visible to the instantiated object, but are visible to classes which inherit from that class, as well as the class itself.

Nothing stops another programmer from opening a class file and changing the access modifiers.

The hiding of data is a good thing because the less you expose, the more you can control and less bugs you can potentially introduce.

like image 32
alex Avatar answered Oct 10 '22 03:10

alex