There is such a php-file with the class:

class.php:

class classTest { private $password; private function showName($foo) { $password= "QWERTY"; $bar = $password. " == " . $foo; return $bar; } private function hideName($foo1) { $password= "QWERTY"; $bar1 = $foo1 . " !!!! " . $password; return $bar1; } } 

He does any nonsense, but it doesn’t matter.

The point is that the $ password variable is used in both functions and its contents are identical.

Tell me, HOW to designate it ONCE somewhere in the class in such a way that it is visible only in the functions of this class, but not visible in the php-file, in which this file with the class is included?

I can not understand where in what cases public is put, where is private ... I broke my head, I just want to show a working and correct example to at least understand how it should work.

  • When instantiating the classTest class, the $password variable will not be visible. It happens in C # and in Java ... - Egor Randomize

2 answers 2

You in the showName and hideName functions enter the local variable $ password , which is not available to the other functions of the class.

In order to use the $ password field of an object in all its functions:

  1. Declare a variable in the class (what you did right)
  2. Access this variable through a $ this class object (what you did wrong)

Here is the correct option:

 class classTest { private $password; private function showName($foo) { $this->password= "QWERTY"; $bar = $this->password. " == " . $foo; return $bar; } private function hideName($foo1) { $this->password= "QWERTY"; $bar1 = $foo1 . " !!!! " . $this->password; return $bar1; } } 

The private modifier means that this variable can only be accessed inside our class (in our case, it is a function of the showName and hideName class ).

Those. if we create a class object

 $a = new classTest(); 

then we will not be able to see and change its variable outside the class

If we set public , we can change and get the class variable outside the functions of this class:

 echo $a->password; // Выводим пароль $a->password = '12345' // Меняем пароль 
  • Thank you very much! It all worked. - Fedor Frost

Label Private. In general, the manual for PHP has not yet been closed :) There are many examples and everything is described in great detail. Go through the examples and everything will become clear, because these are the basics that it is desirable to know by heart for the developer http://php.net/manual/ru/language.oop5.visibility.php

  • Thank! I reread a lot of manas, but for some reason it was through your link that I went in and everything immediately fell into place! Sometimes it happens)) - Fedor Frost