Writing code today is a matter of discovering:
The PHP class member variable cannot accept the return value of the method
Do the following tests:
bin_2.php
<?PHP/** * Created by Phpstorm. * User:administrator * date:14-12-4 * Time: PM 9:45*/Header ("Content-type:text/html;charset=utf-8"); Require_once ('bin_1.php'); Require_once ('bin_3.php');classbin_2{//$bin _3 = new Bin_3 (); //$bin _3->getstr_1 (); Public$testString; //Public $testString _1 = Getstr (); //Public $testString _2 = Test (); //Public static $testString _3 = Self::test_1 (); Publicfunction Test () {echo getstr (); } Public Staticfunction test_1 () {return "ABC"; }} $class=Newbin_2 (); $class->test ();
bin_1.php
<? php function Getstr () { return " Test member variable can accept method return value ";} ? >
bin_3.php
<? PHP /* */class bin_3{ function getstr_1 () {return' ABC"; }}
A class member variable cannot be assigned to a method return value has been tested on
But a bit of a paradox:
First, ordinary member variables, which cannot be assigned, are normal because these variables belong to the object.
$bin _3 = new Bin_3 ();
$bin _3->getstr_1 ();
This indicates that within the class, the object cannot be instantiated outside the member method. But when a class is instantiated, why can't you instantiate another class to implement some business requirements for yourself during the compilation process? You might say that you inherit the class directly, but if you rely on more than one class, PHP does not support multiple inheritance.
Public $testString _1 = Getstr ();
Member variables can not use other files to get a method, this also does not justify, because bin_1.php is not a class, import, change the file will have bin_1 all. Why can't I use my own internal methods to assign values to my own variables? As with the constructor method, you initially assign a value to your member variable. However, this still has shortcomings, feel the importance of the construction method of this thing is far weakened, and caused the code to be difficult to maintain, because it is possible that you use n such a method, but if the construction method, is equivalent to encapsulating all, why not.
Public $testString _2 = Test ();
Does not support, call itself a method, this is also the correct idea, because in the process of instantiation, or brother closed process, but the object itself, and not be designated tasks, such as calling such a method and so on, so since is closed, why not directly equal, and to write a method?
public static $testString _3 = Self::test_1 ();
Static variables, cannot be assigned by their own static methods, presumably this is the same as above the meaning of it.
But in everyday code writing, perhaps a member variable depends on other kinds of business support, then we have to put it in the construction method. Increase the maintainability of the code. Higher packaging.
The PHP class member variable specification has some understanding.