namespace is in the wrong folder

Posted on 2014-01-16
Last Modified: 2014-01-17
from php oop tuturial

when all the code samples are in the same folder for chapter6

because I get error

Warning: require(MyNamespace/Company/Company.php): failed to open stream: No such file or directory in C:\wamp\www\phpvqp3_scripts\ch06\namespace.php on line 13

Fatal error: require(): Failed opening required 'MyNamespace/Company/Company.php' (include_path='.;C:\Program Files (x86)\NuSphere\PhpED\php54\..\include_lib') in C:\wamp\www\phpvqp3_scripts\ch06\namespace.php on line 13

<!doctype html>
<html lang="en">
    <meta charset="utf-8">
    <link rel="stylesheet" href="style.css">
<?php # Script 6.10 - namespace.php
//  This page defines and uses the Department and Employee classes. 

// Include the PHP script:

// Create a department:
$hr = new \MyNamespace\Company\Department('Accounting');

// Create employees:
$e1 = new \MyNamespace\Company\Employee('Holden Caulfield');
$e2 = new \MyNamespace\Company\Employee('Jane Gallagher');

// Add the employees to the department:

// Delete the objects:
unset($hr, $e1, $e2);


Open in new window

<?php # Script 6.9 - Company.php
// This script defines the Company namespace, with two classes.

// Declare the namespace:
namespace MyNamespace\Company;

# ***** CLASSES ***** #
class Department {
    private $_name;
    private $_employees;
    function __construct($name) {
        $this->_name = $name;
        $this->_employees = array();
    function addEmployee(Employee $e) {
        $this->_employees[] = $e;
        echo "<p>{$e->getName()} has been added to the {$this->_name} department.</p>";
} // End of Department class.

class Employee {
    private $_name;
    function __construct($name) {
        $this->_name = $name;
    function getName() {
        return $this->_name;
} // End of Employee class.

# ***** END OF CLASSES ***** #

Open in new window

Question by:rgb192
  • 2
  • 2
LVL 33

Expert Comment

ID: 39787377
greetings rgb192, , it looks like you have gotten confused about how in the PHP CODE Class name spaces work and are used.

The Class namespace has nothing to do with the file system so if you have a namespace as -
Products\View\Output for the view-output.php  file

this DOES NOT MEAN that the Disk location for the view-output.php file is
in the directory  like Products\View\Output
require('Products/View/Output/view-output.php); // ERROR -  failed to open stream

INSTEAD the  view-output.php file is in the folder where you have placed ALL of the namespace CLASS file, maybe folder "class" or folder "includes" or any other folder name -
require('class/view-output.php); // WORKS,
    because the view-output.php is in this folder, and so is the namespace Class file view-arrangement.php for
require('class/view-arrangement.php); // WORKS,

It is possible to have complex namespace setups with hundreds of namespases, so you have to "Organize by folder names"
and have other folders with some namespace reference names like product , model, view  etc.

rgb192, , Namespaces are ONLY NEEDED in very large complex team of many code writers situations, the term means -"namespaces are a way of encapsulating items", and the entire CLASS of PHP is already an effective way to "encapsulate" and group together code sections.
namespace is suppose to - "Avoid Name collisions between the code your team creates", However the PHP Class without Namespaces already does a good job of that, UNLESS there are thousands  and thousands of property and methods names being created by many different people.
I do not believe at this point you will learn anything useful from learning about namespaces, you may should spend your time on other things?

Author Comment

ID: 39787386
in namespace.php
line 12-13
// Include the PHP script:


// Include the PHP script:

Fatal error: require(): Failed opening required 'MyNamespace/Company/Company.php' (include_path='.;C:\Program Files (x86)\NuSphere\PhpED\php54\..\include_lib') in C:\wamp\www\phpvqp3_scripts\ch06\namespace.php on line 13

The Class namespace has nothing to do with the file system so if you have a namespace as -
Products\View\Output for the view-output.php  file

Which namespace are you writing about that is not using the file system?
Is it namespace.php

// Create a department:
$hr = new \MyNamespace\Company\Department('Accounting');

// Create employees:
$e1 = new \MyNamespace\Company\Employee('Holden Caulfield');
$e2 = new \MyNamespace\Company\Employee('Jane Gallagher');
LVL 33

Assisted Solution

Slick812 earned 250 total points
ID: 39787535
you ask - "Which namespace are you writing about that is not using the file system?"
ALL NAME SPACES have NOTHING to do with the file system! !
the PHP  include  AND require   USE the file system as -
require('Company.php');  // WORKS
because Company.php in in the same folder as the php script that is running.
but just because some Namespace has divisions as -
ABSOLUTELY DOES NOT CHANGE THE DISK LOCATION of the Company.php, it does not move it to another folder.

You DO NOTt have the correct understanding of Namespaces used in PHP as way to distinguish TWO of the same NAMES used for different code functions if you have a view method called CheckUser($user);
and a model method called CheckUser($user);   you DO NOT need any namespaces, you just call the same method NAME in two different Objects as -
and then
even though they have the same exact method name, they do a different work in the VIEW class and the MODEL class. So you do not get them mixed up.

With Namespaces it's the same Idea, have the same Names for methods, properties but you can do the method Call separations by using the Space-Separator-Naming of Namespaces as in -
$e1 = new \MyNamespace\COMPANY\Employee('Holden Caulfield');
$e2 = new \MyNamespace\GOVERNMENT\Employee('Holden Caulfield');

Please notice that the to objects $e1 and $e2 are made with the SAME method name as "Employee()"
BUT these are TWO different Classes used, , one is the "COMPANY" class under Class-location or NameSpace of "MyNamespace".
the other is the "GOVERNMENT" class under Class-location or NameSpace of "MyNamespace".
So for purposes of Class-locations in the Namespace Class Organizer, you can never get the "Employee()" method of the "COMPANY" class, and the "Employee()" of the "GOVERNMENT" class mixed up to use the wrong Class,,  much the same as -
$COMPANY->Employee('Holden Caulfield');
$GOVERNMENT->Employee('Holden Caulfield');
would do.

But when you use an Object creation wid Namespace  Class Organizer -
$hr = new \MyNamespace\Company\Department('Accounting');
the  \MyNamespace\Company\Department
IS NOT a DISK file location, NO WAY, Not Possible! !
it just is a complex and confusing way to group together different Classes and keep them separated in a "Tree Branch" organization.
Again, you will never need this crazy concept of Namespaces untill you are a real high expert in writing Classes, so why learn it? ? ?
LVL 109

Accepted Solution

Ray Paseur earned 250 total points
ID: 39788432
Start your learning adventures here, then go back to Ullman's book.

The behavior of PHP namespaces changes, depending on the release of PHP you are using.

The authors of PHP did us a disservice when they used the \ as the namespace designator, because that symbol is easily confused with DIRECTORY_SEPARATOR.

Author Closing Comment

ID: 39789962
Directory separation. And models, views. Thanks for both.

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Selecting SUM data from mysql table with PHP 4 19
echo paypal data on screen 5 51
tutorial for ebay api 3 34
two ways encryption with php 3 28
Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
This article discusses how to create an extensible mechanism for linked drop downs.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question