Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP manual OOP visibility example - can someone explain it

Tags:

oop

php

I saw this in the PHP OOP manual http://www.php.net/manual/en/language.oop5.visibility.php and I can't get my head around why the output is not: Foo::testPrivate Foo::testPublic

class Bar 
{
    public function test() {
        $this->testPrivate();
        $this->testPublic();
    }

    public function testPublic() {
        echo "Bar::testPublic\n";
    }

    private function testPrivate() {
        echo "Bar::testPrivate\n";
    }
}

class Foo extends Bar 
{
    public function testPublic() {
        echo "Foo::testPublic\n";
    }

    private function testPrivate() {
        echo "Foo::testPrivate\n";
    }
}

$myFoo = new foo();
$myFoo->test(); // Bar::testPrivate 
                // Foo::testPublic
like image 333
camden_kid Avatar asked Feb 24 '12 15:02

camden_kid


People also ask

What is PHP visibility?

Visibility ¶ The visibility of a property, a method or (as of PHP 7.1. 0) a constant can be defined by prefixing the declaration with the keywords public , protected or private . Class members declared public can be accessed everywhere.

What is the importance of class visibility in OOP PHP?

Visibility is a big part of OOP. It allows you to control where your class members can be accessed from, for instance to prevent a certain variable to be modified from outside the class. The default visibility is public, which means that the class members can be accessed from anywhere.

What is visibility in object oriented programming?

18.2. Visibility. In common usage, visibility is the ability of an object to “see” or have a reference to another object.

What is different types of visibility or what are access modifiers in PHP?

There are three access modifiers: public - the property or method can be accessed from everywhere. This is default. 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.


2 Answers

It's all about the visibility of the variables / methods.

You'll notice that in the Bar class, the method testPrivate() is private. That means that ONLY itself can access that method. No children.

So when Foo extends Bar, and then asks to run the test() method, it does two things:

  1. It overrides the testPublic() method because it's public, and Foo has the right to override it with it's own version.
  2. It calls test() on Bar (since test() only exists on Bar()).

testPrivate() is not overridden, and is part of the class that holds test(). Therefore, Bar::testPrivate is printed.
testPublic() is overridden, and is part of the inheriting class. Therefore, Foo::testPublic is printed.

like image 129
rockerest Avatar answered Nov 20 '22 10:11

rockerest


In some cases, it is easy to notice that you want a private method on the Bar class, but you also wants the Foo class to access it.
But wait, it is public or private?
Here comes the protected modifier.
When a method is private, only the class itself can call the method.
When a method is public, everyone can call it, like a free party.
When a method is protected, the class itself can call it and also whoever inhered this method (children) will be able to call it as a method of their own.

like image 37
Marco Aurélio Deleu Avatar answered Nov 20 '22 12:11

Marco Aurélio Deleu