简体   繁体   中英

include methods from external php file in a class

I have a class with own methods and on the other side a php file what contains external methods. From documentation is clear that inside of a class includeing external functions is not possible

How could I inculde this functions in my class. Making an another class and extend my first class it can not be an option.

You can't. All class definition, including methods and fields must be on the same file. You can't declare the same class in two different files.

Extending, or using traits (if you have PHP 5.4.x+), are your only options.

You can call external functions from a class, even if they are not enclosed in a class of their own:

Global.php

<?php
function doSomething() {
    return 'Hello';
}
?>

ExampleClass.php

<?php
include_once('Global.php');

class ExampleClass
{
    public function example() {
        return doSomething();
    }
}
?>

Although you probably wouldn't have the include in the actual class file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM