Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
php通过autoloader调用函数_Php_Oop - Fatal编程技术网

php通过autoloader调用函数

php通过autoloader调用函数,php,oop,Php,Oop,这些是我的课 class DB{ // returns the instance as it should - all system works well public function create(){ // creates a row in the db.... } } 我有一个自动加载器,可以加载类,系统运行良好 $user = new User(); $user->create(....) 我想在实例化用户类时使用从DB创建的方

这些是我的课

class DB{

    // returns the instance as it should - all system works well

    public function create(){
      // creates a row in the db....    
    }
}

我有一个自动加载器,可以加载类,系统运行良好

$user = new User();
$user->create(....)
我想在实例化用户类时使用从DB创建的方法,我怎么做

我想阻止的事情是不必在每个需要更新的类中创建函数“update”

例如:

类用户-更新“用户表” 班级儿童-更新“儿童表”

反而

// from DB class
update($table_name, $field)

自动加载程序负责查找并包含包含类定义的文件,这样您就不必显式调用
include'db.php'等等,但它将无法执行您在这里尝试执行的操作

如果
create()
方法是在
DB
类中定义的,那么没有理由通过另一个类(如
User
)的实例来访问它,除非您专门做了一些事情来允许它,否则自动加载程序将不会对此提供帮助

User
实例访问
DB::create()
方法的唯一方法是
User
扩展
DB

class User extends DB { ...
这意味着所有
User
对象也将是
DB
的实例,并且可以访问
DB
的所有公共和受保护方法,而不仅仅是
create()


看起来这可能是你想要的。如果是这样的话,请仔细阅读。

尝试了一系列操作,得到了未定义的方法$user->create,因为我试图将其移动到DB类。据我所知,$user=新用户可以使用autoloader访问所有其他类,所以当我尝试从DB调用函数时,它应该会出现…可能这就是我所缺少的理解
class User extends DB { ...