简体   繁体   English

如何在PHP中的另一个内部使用类?

[英]How I use a class inside another in PHP?

I have two classes. 我有两节课。 UserModel and UserController. UserModel和UserController。

file: localhost/controllers/user.controller.php 文件:localhost / controllers / user.controller.php

<?php
class UserController
{
    public function GetArray() {
        $query = 'SELECT id, username, email, password FROM Users';
        $result = mysql_query($query);
        $model = new UserModel();
        while ($row = mysql_fetch_array($result)) {
            $model->id = $row['id'];
            $model->username = $row['username'];
            $model->email = $row['email'];
            $model->password = $row['password'];
            $array[] = $model;
        }
        return $array;
    }
}
?>

The UserModel class is in another file: localhost/models/user.model.php. UserModel类位于另一个文件中:localhost / models / user.model.php。 The 6th row is causing the error, because there's no references to the other file. 第六行导致错误,因为没有对另一个文件的引用。 How can I do that? 我怎样才能做到这一点?

Sorry for my bad english, and thanks to you all. 对不起,我的英语不好,谢谢大家。

使用requirerequire_once包括其他文件。

See the following link. 请参阅以下链接。 Use any of th method to call a class inside another class. 使用任何一种方法可以在另一个类中调用一个类。 Thanks 谢谢

Calling a class inside another one in PHP 在PHP中调用另一个内的类

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

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