A couple of questions about OO and PHP classes

I study OO and classes, I have a couple of questions about OO and classes in PHP

  • As I understand it, a class that extends another class simply means that a class that extends another class has access to the variables / properties and functions / methods of the class from which it extends. Is it correct?

  • I know that a static method or property is basically the same as a procedural function or a variable outside the class, and they can be used almost everywhere. Is it correct?

  • Public means that any class can access it, and Private means that access and use is only for a class that is encapsulated or for a class that is distributed from the owner of the access. Is it correct?

+3
source share
3 answers

1) Yes, that’s right. Children's class inherits all the properties and methods protected, or publicits parent. All advertised privatecannot be used.

2) This is true. As long as the class is loading (this applies well to your startup issue), you can access the static methods using the scope resolution operator (: :), for example:ClassName::methodName();

3) You have the value publiccorrectly, but, as I mentioned earlier, methods privatecan only be used by the class in which they are declared.

class parentClass
{
     private $x;
     public $y;
}

class childClass extends parentClass
{    
    public function __construct() {
        echo $this->x;
    }
}

$z = new childClass();

The code above will trigger an error NOTICEbecause $ x is not available for childClass.

Note: Undefined property: ChildClass :: $ x

$x protected, childClass . : , protected, , , , , . public private.

+11

1. , , , , , , / / , . ?

ANS: , . , . , , .

2. , , , . ?

ANS: , , , , . , .

3. , , Private , , , . ?

ANS: , ( ) , () .

Zambat.

0

Very little need to be declared private, since a general rule is used instead.

-2
source

Source: https://habr.com/ru/post/1717405/


All Articles