Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Confused with php namespaces

Tags:

namespaces

php

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?

like image 732
user2877011 Avatar asked Oct 14 '13 18:10

user2877011


People also ask

What is the purpose of namespace in PHP?

Namespaces are qualifiers that solve two different problems: They allow for better organization by grouping classes that work together to perform a task. They allow the same name to be used for more than one class.

What is the best approach for working with classes and namespaces in PHP?

To address this problem, namespaces were introduced in PHP as of PHP 5.3. The best way to understand namespaces is by analogy to the directory structure concept in a filesystem. The directory which is used to group related files serves the purpose of a namespace.

What is the difference between namespace and use in laravel?

The use keyword allows the developers to shorten the namespace. The namespace once created can include various functionalities which can be used in controllers and various classes.

Can we use two namespaces in PHP?

Defining multiple namespaces in the same file ¶Multiple namespaces may also be declared in the same file. There are two allowed syntaxes. This syntax is not recommended for combining namespaces into a single file. Instead it is recommended to use the alternate bracketed syntax.


2 Answers

  1. You cannot import a namespace (import user). You need to import concrete classes instead (use user\Member)
  2. There is no need of importing Member, since it’s in the same namespace
  3. use statement does not include files. You must consider some kind of autoloading. Consider PSR-0 and/or Composer

To solve your problem ad hoc, use include:

include '../Member.php';
like image 125
Maciej Łebkowski Avatar answered Sep 19 '22 10:09

Maciej Łebkowski


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

like image 41
Adam Rivers Avatar answered Sep 19 '22 10:09

Adam Rivers