无法访问另一个类

时间:2014-06-29 07:26:39

标签: php

我的问题是,当我运行此代码时:

<?php
    require_once('classes/class.users.php');

    use Users\UserService;
    use Users\UserMapper;


    if(isset($_POST['login'])) {
        $result = $userService->handleUserLogin($_POST['username'], $_POST['password']);
    }
?>

我收到此错误:

Notice: Undefined variable: userMapper in C:\xampp\htdocs\Projects\SMS\classes\class.users.php on line 8

Fatal error: Call to a member function checkLogin() on a non-object in C:\xampp\htdocs\Projects\SMS\classes\class.users.php on line 8

我的全班来源:http://pastebin.com/8nahRnrT

2 个答案:

答案 0 :(得分:0)

感谢Ronni帮助我。

我刚删除了DB类,我改为创建mysqli类的实例并将其传递给UserMapper类。我还将UserMapper类实例传递给UserService类,它修复了问题。

$db          = new mysqli('localhost', 'root', 'php123', 'sms');
$userMapper  = new UserMapper($db);
$userService = new UserService($userMapper);

答案 1 :(得分:-1)

$userMapper->checkLogin($username, $password) === true)

这一行,它在你UserService类中被调用。

UserMapper从未在UserService类中启动。

也许在构造函数中启动它并为其添加属性