How to use a PHP class from another file?

前端 未结 5 1664
庸人自扰
庸人自扰 2020-12-05 06:41

Let\'s say I\'ve got two files class.php and page.php

class.php



        
5条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-05 06:56

    You can use include/include_once or require/require_once

    require_once('class.php');
    

    Alternatively, use autoloading by adding to page.php

    data);    
    ?>
    

    It also works adding that __autoload function in a lib that you include on every file like utils.php.

    There is also this post that has a nice and different approach.

    Efficient PHP auto-loading and naming strategies

提交回复
热议问题