Reputation: 110
I've got the following structure in my app:
|
|----user folder
| |
| |-----Member.php
| |
| |---- tests Folder
| |
| |-----Member.php
Here is my Member.php
in user folder:
<?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;
}
}
And here is my Member.php in tests folder:
<?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("[email protected]");
if($member->getEmail() == "[email protected]")
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";
When I want to create an Object it throws exception and says:
Fatal error: Class 'user\Member' not found in C:\xampp\htdocs\auto24\user\tests\Member.php on line 8
My question is how can I call the Member Object in the tests folder files using namespaces?
Upvotes: 6
Views: 587
Reputation: 1075
You need to include the namespace and the class name: use user\Member;
Member.php:
namespace user;
class Member
{
private $firstName;
private $lastName;
private $email;
private $password;
private $cell;
//and some other stuff
}
Member.php in tests folder:
namespace user\tests\user;
use user\Member;
$member = new Member();
I'm presuming you also need to add the following to Member.php
in tests folder if you haven't:
include '../Member.php';
Alternatively you could also use an autoloader to load classes/namespaces on the fly.
PHP Autoloading classes: http://www.php.net/manual/en/function.spl-autoload-register.php
Upvotes: 2
Reputation: 3887
import user
). You need to import concrete classes instead (use user\Member
)Member
, since it’s in the same namespaceuse
statement does not include files. You must consider some kind of autoloading. Consider PSR-0 and/or ComposerTo solve your problem ad hoc, use include:
include '../Member.php';
Upvotes: 8