Php-php advanced-php class some functions or methods can be imported through include. How can this problem be achieved?

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.