6

我的应用程序中有以下结构:

|
|----user folder
|        |
|        |-----Member.php
|        | 
|        |---- tests Folder
|                  |
|                  |-----Member.php

这是我Member.php的用户文件夹:

<?php

namespace user;

class Member
{
    private $firstName;
    private $lastName;
    private $email;
    private $password;
    private $cell;

    public function __construct()
    {

    }

    public function getFirstName()
    {
        return $this->firstName;
    }

    public function setFirstName($firstName)
    {
        $this->firstName = $firstName;
        return TRUE;
    }

    public function getLastName()
    {
        return $this->lastName;
    }

    public function setLastName($lastName)
    {
        $this->lastName = $lastName;
        return TRUE;
    }

    public function getEmail()
    {
        return $this->email;
    }

    public function setEmail($email)
    {
        $this->email = $email;
        return TRUE;
    }

    public function getPassword()
    {
        return $this->password;
    }

    public function setPassword($password)
    {
        $this->password = $password;
        return TRUE;
    }

    public function getCell()
    {
        return $this->cell;
    }

    public function setCell($cell)
    {
        $this->cell = $cell;
        return TRUE;
    }
}

这是我在测试文件夹中的 Member.php:

<?php
/*
 * Member class getters and setters tests
 */
namespace user;
use user;

$member = new Member();
$member->setFirstName("Javad");
if($member->getFirstName() == "Javad")
    echo "Test code 1 passed";
else
    echo "Test code 1 failed";

$member = new Member();
$member->setLastName("Akbari");
if($member->getLastName() == "Akbari")
    echo "Test code 2 passed";
else
    echo "Test code 2 failed";

$member = new Member();
$member->setEmail("j2omrani@gmail.com");
if($member->getEmail() == "j2omrani@gmail.com")
    echo "Test code 3 passed";
else
    echo "Test code 3 failed";

$member = new Member();
$member->setPassword("123456");
if($member->getPassword() == "123456")
    echo "Test code 4 passed";
else
    echo "Test code 4 failed";

$member = new Member();
$member->setCell("09121234567");
if($member->getCell() == "09121234567")
    echo "Test code 5 passed";
else
    echo "Test code 5 failed";

当我想创建一个对象时,它会抛出异常并说:

Fatal error: Class 'user\Member' not found in C:\xampp\htdocs\auto24\user\tests\Member.php on line 8

我的问题是如何使用命名空间调用测试文件夹文件中的成员对象?

4

2 回答 2

8
  1. 您不能导入命名空间 ( import user)。您需要导入具体的类 ( use user\Member)
  2. 不需要 import Member,因为它在同一个命名空间中
  3. use声明不包括文件。您必须考虑某种自动加载。考虑PSR-0和/或Composer

要临时解决您的问题,请使用包括:

include '../Member.php';
于 2013-10-14T18:20:51.000 回答
2

您需要包括命名空间和类名:use user\Member;

会员.php:

namespace user;
class Member
{
    private $firstName;
    private $lastName;
    private $email;
    private $password;
    private $cell;
    //and some other stuff
}

测试文件夹中的 Member.php:

namespace user\tests\user;
use user\Member;

$member = new Member();

Member.php如果您没有,我假设您还需要在测试文件夹中添加以下内容:

include '../Member.php';

或者,您也可以使用自动加载器动态加载类/命名空间。

PHP 自动加载类:http ://www.php.net/manual/en/function.spl-autoload-register.php

于 2013-10-14T18:19:23.643 回答