I have PHP namespaced file
namespace A\B\C
$cls = new foo();
Where is the foo class stored?
OR if exist __autoload() how to control directory mapping to namespace?
Thanks for helping. Im trying to closer understand relation between directories and namespaces. PHP and/or Google doesnt give me any answer.
There is not direct relation between directories and namespaces, except if you use an autoloader.
If you want this code to works :
namespace A\B\C;
$cls = new foo();
You must do this :
namespace A\B\C;
class foo {}
$cls = new foo();
Or do this :
foo.php
namespace \A\B\C;
class foo {}
index.php
require 'foo.php';
$cls = new \A\B\C\foo();
// or
namespace A\B\C;
$cls = new foo();
Now if you want to use autoloader, you can use something like this :
function autoload($class)
{
$class = str_replace('\\', '/', $class);
require_once __DIR__ . '/' . $class . '.php';
}
spl_autoload_register('autoload');
Put this kind of code in top of your index.php and the get a dir arch like this :
A
+ B
| + C
| | + foo.php
Then you just have to do :
$cls = new \A\B\C\foo();
or even :
use \A\B\C\foo;
$cls = new foo();
But anyway, YOU MUST READ MANUAL : Link here
Related
I have found several SO questions similar to mine, but am struggling to find an answer that helps me, plus I'd really like to know the best practice for autoloading classes that exist within namespaces.
My folder structure:
root
-- classes
--- Users
---- Users.class.php
And users.php;
<?php
namespace CompanyName\ProjectName\Users;
class UserMapper
{
// class code here
}
And my autoload function, which sits in the root folder;
/* autoload classes on instatiation */
spl_autoload_register(function($class)
{
include $_SERVER['DOCUMENT_ROOT'] . '/classes/' . $class . '.class.php';
});
And, let's say I call the user class like so;
<?php
new \CompanyName\ProjectName\User();
Warning: include(/Applications/XAMPP/xamppfiles/htdocs/test_tool/classes/CompanyName\ProjectName\User.class.php): failed to open stream: No such file or directory in...etc
To use spl_autoload_register, do I need to map my folder structure to my namespace structure? I would prefer not to do this as I like to have my classes in the same folder, with sub folders within.
Or do I add extra code to my autoload function?
I have also searched the php manual, and there is no working namespace example, which I find very strange.
Any help would be much appreciated.
Thanks in advance.
Disclaimer: I am a beginer at php, my answer may not be correct
but i'm confident examining and testing the example bellow will help clarify the use of Namespaces with spl_autoload_register for any beginner like me.
Consider this folder structure :
root/ contains index.php.
root/model/ contains A.php & AA.php
A.php :
?php
namespace company\model;
use \company\model\A;
class A
{
public function speak()
{
echo 'hello world! ';
}
}
AA.php :
?php
namespace company\model;
use \company\model\A;
require_once 'A.php';
class AA extends A
{
public function shout()
{
echo 'HELLO WOORLD!!!';
}
}
index.php :
<?php
namespace company;
use \company\model\A;
function classLoader ($className)
{
if (file_exists($className.'.php'))
{
require_once $className.'.php';
} else {
$className = str_replace('\\', '/', $className);
$className = str_replace('company/', '', $className);
if (file_exists($className.'.php'))
require_once $className.'.php';
else
throw new EXCEPTION('classLoader could not find '.$className.'.php .');
}
}
spl_autoload_register(classLoader);
$obj = new A;
//we dont need to write ($obj = new \company\model\A;)
//because of statement at line 4
$obj->speak();
echo '<br/>';
$objA = new \company\model\AA;
$objA->shout();
echo '<br/>';
class AB extends \company\model\AA
{
public function doBoth()
{
$this->speak();
$this->shout();
}
}
$objB = new AB;
$objB->doBoth();
I'm trying to update my project with namespaces.
Before using namespaces I could easily call a class via a variable like so:
<?php
$className = "Item";
$myClass = new $className();
?>
Now, when using namespaces (with use) I'd expect this to work:
(not working allways meens: 'Can't find class' or smth like this)
<?php
namespace myProject
use myProject\Models
$className = "Item";
$myClass = new $className(); // this class has the namespace myProject\Models
// this doesn't work! (can't find class)
$myClass = new Models\$className();
// nope, doesn't work either
$myClass = new myProject\Models\$className();
// nope
?>
but calling the same class directly - without a variable - IS working
$myClass = new Item();
What I have to do, to make it work is:
<?php
namespace myProject;
use myProject\Models; // unnessecary now
$className = "\\" . __NAMESPACE__ . "\\Models\\" . "Item";
$myClass = new $className();
?>
My question now is:
If I want to call a class via a variable, do I really have to include the whole namespace as a fully qualified name?
EDIT:
I found myself an answer here:
Dynamic namespaced class with alias
I'll keep that question anyway, because I could not find the right answer in first place.
I am creating an application in CakePHP 3.0, in this application I want to draw SVG graphs of data using a php class that I have written. What would be the proper way to go about using this class in my CakePHP 3 project?
More specifically:
What are the naming conventions? Do I need to use a specific namespace?
Where do I put the file that contains the PHP class?
How can I include it and use it in a controller or a view?
What are the naming conventions? Do I need to use a specific namespace?
Your SVG graphs class should have a namespaces. For namespaces you can see http://php.net/manual/en/language.namespaces.rationale.php
Where do I put the file that contains the PHP class?
Create a folder by author(here might be your name, as you are the author) in vendor
Then create your class inside of it
convention is vendor/$author/$package . You can read more http://book.cakephp.org/3.0/en/core-libraries/app.html#loading-vendor-files
How can I include it and use it in a controller or a view?
a) To include:
require_once(ROOT .DS. 'Vendor' . DS . 'MyClass' . DS . 'MyClass.php');
(replace MyClass by your foldername and MyClass.php by your filename.php)
b) To use it:
add use MyClass\MyClass; in your controller
For example I want to add MyClass in a controller. Steps that worked for me
Creating vendor\MyClass folder
Pasting MyClass.php in that folder
adding namespace MyClass; at the top of MyClass.php
MyClass.php have following code for example:
namespace MyClass;
class MyClass
{
public $prop1 = "I'm a class property!";
public function setProperty($newval)
{
$this->prop1 = $newval;
}
public function getProperty()
{
return $this->prop1 . "<br />";
}
}
Adding use MyClass\MyClass; at the top of controller
Then including it in my controller action. My action sample
public function test()
{
require_once(ROOT .DS. "Vendor" . DS . "MyClass" . DS . "MyClass.php");
$obj = new MyClass;
$obj2 = new MyClass;
echo $obj->getProperty();
echo $obj2->getProperty();
exit;
}
recently I was learning zend framework 2, and there's a problem annoying me for a long time, things look like this:
<?php
namespace Album\Model;
// Add these import statements
use Zend\InputFilter\InputFilter;
use Zend\InputFilter\InputFilterAwareInterface;
use Zend\InputFilter\InputFilterInterface;
class Album implements InputFilterAwareInterface
{
public $id;
public $artist;
public $title;
protected $inputFilter;
public function exchangeArray($data)
{
$this->id = (isset($data['id'])) ? $data['id'] : null;
$this->artist = (isset($data['artist'])) ? $data['artist'] : null;
$this->title = (isset($data['title'])) ? $data['title'] : null;
}
// Add content to these methods:
public function setInputFilter(InputFilterInterface $inputFilter)
{
throw new \Exception("Not used");
}
//....
?>
This code was a section of the "skeleton application" programme, which was a tutorial of ZF2. The first time I see the programme, I don't understand what's the usage of "namespace" and "use", because this two keyword doesn't exist in php5.2(also the same in the earlier edition), so I go to see the manual and try to understand it.I write a programme to simulate what really happens:
<?php
use script\lib\test;
$o = new test();
echo $o->getWelcome();
function __autoload( $className ) {
$classname = strtolower( $classname );
require_once( dirname( __FILE__ ) . '/' . $classname . '.php' );
}
?>
the programme above works well, of course I created two folders named script and lib, and there's a file named test.php.
Seems like every thing is clear, zend framework also has a autoload function, BUT when I noticed the codes in "skeleton application programme", there was a namespace in the beginning, so I adds the namespace to my programme too:
<?php
namespace test;
use script\lib\test;
$o = new test();
echo $o->getWelcome();
function __autoload( $className ) {
$classname = strtolower( $classname );
require_once( dirname( __FILE__ ) . '/' . $classname . '.php' );
}
?>
the page returned me inforamtion as following:
Fatal error: Class 'script\lib\test' not found in E:\wamp\www\test\test_29.php on line 6
I tried to change the namespace's name such as script\lib, script\lib\test...
but it's useless.
Any answer will be appreciated, thanks.
Now I will give you more details about this issue:
To understand the usage of "namespace" and "use", I looked over the materials on php.net:
http://php.net/manual/en/language.namespaces.importing.php
In this page, there was a section of code looks like this:
Example #1 importing/aliasing with the use operator
<?php
namespace foo;
use My\Full\Classname as Another;
// this is the same as use My\Full\NSname as NSname
use My\Full\NSname;
// importing a global class
use ArrayObject;
$obj = new namespace\Another; // instantiates object of class foo\Another
$obj = new Another; // instantiates object of class My\Full\Classname
NSname\subns\func(); // calls function My\Full\NSname\subns\func
$a = new ArrayObject(array(1)); // instantiates object of class ArrayObject
// without the "use ArrayObject" we would instantiate an object of class
?>
Now let's review the programme I write in the above:
<?php
namespace test;
use script\lib\test;
$o = new test();
echo $o->getWelcome();
function __autoload( $className ) {
$classname = strtolower( $classname );
require_once( dirname( __FILE__ ) . '/' . $classname . '.php' );
}
?>
It's the same, I'm trying to simulate that instance, if we don't use the autoload function:
<?php
namespace test;
use script\lib\test;
require_once 'script/lib/test.php';
$o = new test();
echo $o->getWelcome();
?>
It works well too, BUT when I use __autoload function to load the class file, there's something wrong.
I don't konw where's problem, OR any body tried to write an instance to put the "Example #1" into practice? I will wait for your answer.
I think you're misunderstanding what's going on here.
Namespaces allow you to, more or less, create "directories" for your classes. So you can create the \Foo class and the \Test\Foo class (where \ represents the "root" of your application).
The way autoloading works is that your files mirror your namespacing. So foo.php would be in the root of your autoloading but you would create /test/foo.php for \Test\Foo
The use keyword has two uses. One is to alias class files and the other is, in PHP 5.4 or later, to bring in a Trait into your current class.
Now, to your question. First, Let's look at your code
<?php
namespace test;
use script\lib\test;
$o = new test();
echo $o->getWelcome();
This is confusing. You declare a namespace (which you don't need to do here) but then you alias it to script\lib\test. PHP is now looking for a file called /script/lib/test.php, which your error message says doesn't exist. But you said the file does exist so let's look at that
public function getWelcome() {
return 'welcome';
}
This isn't a class. It's a function. For this example you need a complete class
<?php
namespace script\lib;
class test {
public function getWelcome() {
return 'welcome';
}
}
Lastly, let's talk autoloading. You don't need to use use with autoloading. Your autoloader should take care of that for you. You should, however, use spl_autoload_register(), as __autoload() is soon to be depreciated.
From ZF2 docu
Zend\Loader\StandardAutoloader is designed as a PSR-0-compliant autoloader. It assumes a 1:1 mapping of the namespace+classname to the filesystem, wherein namespace separators and underscores are translated to directory separators.
Read more about: PSR-0
So if you're using namespaces the classname that gets send to the autoloader doesn't look like test. It looks like YOUR_NAMESPACE\test. YOUR_NAMESPACE is the namespace that you defined in the class with namespace YOUR_NAMESPACE;
PSR-0 is a standard that says: Your namespace should reflect your filesystem. You only have to replace the backslashes with forward slashes. Or _ with / if you're using pseudo namespaces like in ZF1. (Album_Model_Album)
So output the $className that is sent to your autoloader and you will see..
I'm trying to set up a simple example in order to understand how the ClassLoader Component of Symfony and the new PSR-0 standard work.
First I created Bar.php:
namespace Acme;
class Bar
{
// Implementation
public static function helloWorld() {
echo "Hello world!";
}
}
Then I created a autoloader.php (under the vendor path I have the ClassLoader component):
require_once __DIR__.'/vendor/symfony/class-loader/Symfony/Component/ClassLoader/UniversalClassLoader.php';
use Symfony\Component\ClassLoader\UniversalClassLoader;
$loader = new UniversalClassLoader();
$loader->register();
$loader->registerNamespace('Acme', __DIR__);
Lastly I created Foo.php:
require_once 'autoloader.php';
use Acme;
class Foo extends Bar
{
// Implementation
}
$foo = new Foo();
$foo::helloWorld();
But when I execute:
$ php Foo.php
I get the following error message:
PHP Warning: The use statement with non-compound name 'Acme' has no
effect in Foo.php on line 4
PHP Fatal error: Class 'Bar' not found in Foo.php on line 7
What am I doing wrong here?
UPDATE:
If instead of using namespace Acme I use namespace Acme\Bar in Bar.php, I get the same error message as shown above.
I've found what was going on wrong. The problem was that the UniversalClassLoader class that follows the standard PSR-0, requires that the files with the namespaces cannot be in the root directory, and must be created under a minimum of one directory.
Here is the code in case someone wants to try the example.
autoloader.php
require_once __DIR__.'/vendor/Symfony/Component/ClassLoader/UniversalClassLoader.php';
$loader = new Symfony\Component\ClassLoader\UniversalClassLoader();
$loader->registerNamespaces(array('Acme' => __DIR__ . '/src'));
$loader->register();
./src/Acme/Bar.php
namespace Acme;
class Bar
{
// Implementation
public static function helloWorld() {
echo "Hello world!";
}
}
./src/Acme/Foo.php
namespace Acme;
require_once '../../autoloader.php';
use Acme\Bar;
class Foo extends Bar
{
// Implementation
}
$foo = new Foo();
$foo::helloWorld();
You can't use an entire namespace, you have tu use Acme\Bar.