简体   繁体   English

PHP OOP扩展外部类

[英]PHP OOP extending external class

Im new to OOP in PHP. 我是PHP中OOP的新手。 I just can't extend a class from an external file. 我只是不能从外部文件扩展类。 They are both on the same folder. 它们都在同一文件夹中。

DB.php 数据库

<?php

class DB {
   // some functions here
}

Home.php Home.php

<?php
require_once("DB.php");
class Home extends DB {
   // initialize db and some functions here
}

and I get an error: Fatal error : Class 'DB' not found in \\location\\to\\Home.php on line 3 我得到一个错误: 致命错误 :在第3行的\\ location \\ to \\ Home.php中找不到类'DB'

It should be an absolute path import 它应该是绝对路径导入

require_once __DIR__ . '/DB.php';

Why: because otherwise the relative import is evaluated relatively to your current working directory, which is basically can be anything during the runtime. 原因:因为否则相对导入相对于您当前的工作目录进行评估,这在运行时基本上可以是任何东西。 So it's recommended to always require files with the absolute path. 因此,建议始终要求使用绝对路径的文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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