Solved

namespace is in the wrong folder

Posted on 2014-01-16
5
340 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
  • 2
  • 2
5 Comments
 
LVL 33

Expert Comment

by:Slick812
Comment Utility
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
Comment Utility
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 33

Assisted Solution

by:Slick812
Slick812 earned 250 total points
Comment Utility
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 108

Accepted Solution

by:
Ray Paseur earned 250 total points
Comment Utility
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
Comment Utility
Directory separation. And models, views. Thanks for both.
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Introduction Many web sites contain image galleries; a common design for these galleries includes a page with a collection of thumbnail images.  You can click on each of the thumbnail images to see the larger version of the image.  This is easily i…
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
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 …

762 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now