The PHP property declaration accepts an array and does not accept any other expressions that can be evaluated during compilation.

when studying oop in PHP, I noticed that the property declaration takes an array as the value, the PHP documentation mentioned here

class Test{
      public $var7 = array(true, false);
}     

and I noticed that the documentation says:

This declaration may include initialization, but this initialization must be a constant value, that is, it must be able to be evaluated at compile time and should not depend on runtime information for evaluation.

and after reading this , to find out how the compilation process works, I realized that during the compilation process, some expressions will be evaluated and optimized, if possible, as the snippet below:

var x = strlen ("testing") => int(7);

, , , , ?

     Class Test {
          public $vars=strlen("random"); // Fails
     }
+4
1

, strlen() , array() keyword.

, , .

php.net:

PHP. , , .. - : - . , , .

, , - , .

: , "functions.php".

//functions.php
namespace My_Project_Namespace;

function strlen($string){
    return 10;  //In my project, all strings are length 10! 10 is a nice round number...
} 

strlen() . , namespace ( My_Project_Namespace).

, ( )

//Test.php
namespace My_Project_Namespace;

Class Test {
    public $vars=strlen("random"); // Fails
}

strlen() 2 . , , . strlen(), , , strlen(), !

array() - . , array(), .

+3

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


All Articles