Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

namespace is in the wrong folder

Posted on 2014-01-16
5
Medium Priority
?
356 Views
Last Modified: 2014-01-17
from php oop tuturial
http://www.larryullman.com/books/php-advanced-and-object-oriented-programming/


why
require('MyNamespace/Company/Company.php');
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


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

// Include the PHP script:
require('MyNamespace/Company/Company.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');

// Add the employees to the department:
$hr->addEmployee($e1);
$hr->addEmployee($e2);

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

?>
</body>
</html>

Open in new window


Company.php
<?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

0
Comment
Question by:rgb192
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 34

Expert Comment

by:Slick812
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
Products\View\Arrangement
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"
require('class/views/view-output.php);
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?
0
 

Author Comment

by:rgb192
ID: 39787386
in namespace.php
line 12-13
// Include the PHP script:
require('Company.php');  
works

and

// Include the PHP script:
require('MyNamespace/Company/Company.php');


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');
0
 
LVL 34

Assisted Solution

by:Slick812
Slick812 earned 1000 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 -
MyNamespace\Company
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 -
$view->CheckUser($user);
and then
$model->CheckUser($user);
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');
or
$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');
and
$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? ? ?
0
 
LVL 111

Accepted Solution

by:
Ray Paseur earned 1000 total points
ID: 39788432
Start your learning adventures here, then go back to Ullman's book.
http://php.net/manual/en/language.namespaces.php

The behavior of PHP namespaces changes, depending on the release of PHP you are using.
http://php.net/manual/en/language.oop5.changelog.php

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.
0
 

Author Closing Comment

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

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

618 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