Classa {publicfunctiona () {}include (& #039; file. php & #039;) ;} I want to write this, but it is not supported. Class {
Public function (){}
Include ('file. php ');
}
I want to write it like this, but it is not supported.
Reply content:
Class {
Public function (){}
Include ('file. php ');
}
I want to write it like this, but it is not supported.
First, you need to distinguish between functions and methods. If you want to introduce functions, You can include them in the file header. However, you need to determine whether to define functions. Otherwise, a function redefinition error will be thrown, if the method is introduced, either inherit or use trait to turn it into a member. Otherwise, you can create a corresponding class instance. Note that when creating an instance, the class has been successfully loaded.
class a{ public function a(){ include('file.php'); }}
Suppose you want to introduce a class file and call the class method in it. Now there are two class files, file. php and a. php. The Code is as follows:
File. php
a.php
_ FileObj = $ fileObj ;}}
Class a uses the file method. The Code is as follows:
_ FileObj = $ fileObj ;}}
Pass the file class object to Class:
$file = new a(new File());
Then you can use the file class method in Class.
If you want to use a function in the class, it is simpler. Just introduce Lei Qian to the function file (change the require file in file a to the function file ), because the function registers the root namespace by default.
If you want to load a file in the class. Can be placed in__construct.
For example, a filea.php
index.php
In this way, you can load files in the class.a.php
Because include is a statement, it cannot be directly put into the class and put into the class initialization function.__constructYou can run it automatically.
Your idea is wrong. You should first understand object-oriented.
First, the function does not need to be referenced in the object. The object is the object, and the function is the function. The reason why the function indicates that it may not be explicitly summarized as the object to which it belongs.
Methods are obtained through inheritance and do not need to be included.
For example, if you define a fruit class, this class can have a method to obtain the juice content, but there should be no way to squeeze the juice, it should be provided by the juicer object.
The omnipotent object including all methods does not exist. In this state, the object and no object want to be the same result.