简体   繁体   中英

How I use a class inside another in PHP?

I have two classes. UserModel and UserController.

file: 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. 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

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