SplClassLoader Directory Structure - php

I am new to namespaces and I guess autoloading in the method of SplClassLoader. I've tried search for many tutorials but not having much luck. Perhaps you guys can help me get this going?
Directory Structure
- Oram
- Lib
- Classes
Test.php
- index.php
- SplClassLoader.php
Test.php
<?php
namespace Oram\Lib\Classes;
class Test
{
function __construct()
{
echo "Test Class loaded";
}
}
index.php
<?php
require_once('SplClassLoader.php');
$loader = new SplClassLoader('Lib', 'Oram\Lib');
$loader->register();
use Oram\Lib\Classes\Test;
$test = new Test();
This is all inside localhost/website/ too btw as I am running it on WAMP.
Fatal error: Class 'Oram\Lib\Classes\Test' not found in C:\Program Files\wamp\www\website\index.php on line 10
Any advice or if someone could point me to some reading resources to get my head around this would be great.
Thank you

Edit:
I have it!
I think you have to change the backslash (\) to a slash (/).
<?php
require_once('SplClassLoader.php');
$loader = new SplClassLoader('Lib', 'Oram/Lib');
$loader->register();
use Oram\Lib\Classes\Test;
$test = new Test();
?>
Take a look on [this][1].
Try that. I have added a \ before your namespace. That could be the answer:
$loader = new SplClassLoader('Lib', '\Oram\Lib');

Related

Trying to learn about namespaces and it does not seem to work, class is not found

I just do not seem to be able to wrap my head around why this does not seem to be working. I'm trying to learn about namespaces, and going from the book to the computer, thinking this would just be easy, I've been occupied with this for the last one and a half hour now and just do not get what I am doing wrong.
Here's the deal.
I'm running MAMP Pro on my MacBook from a folder on my desktop called 'learning-php'.
In side this folder there are two files.
/Index.php
/Classes/Person.php
These two files look like this:
File: index.php
<?php
use Classes\Person;
$person = new Person();
?>
File: Classes/Person.php
<?php
namespace Classes;
class Person
{
//code goes here...
}
?>
This results in this error:
Fatal error: Uncaught Error: Class 'Classes\Person' not found in /Users/John/Desktop/learning-php/index.php:5 Stack trace: #0 {main} thrown in /Users/John/Desktop/learning-php/index.php on line 5
Use include, it will pull the entire file into the file you are incuing it into so you could make an instance of the class anywhere after your include statement. You will need to have a way to find the full file path to the file however, since it is not going to take it from the root of your application.
Documentation here.
include '/Users/John/Desktop/learning-php/Classes/Person.php';
$person = new Person();
Using something like this will allow you to auto-load the classes. To test just include it in the top of your PHP page after declaring the namespace.
<?php
namespace Test;
spl_autoload_register(function($className){
include '../'.$className.'.php';
});
$obj = new Test(); //this loads from Test/Foo/Bar/Test.php

Composer autoload is called but loaded nothing

Project structure
/Test
composer.json
composer.lock
index.php
nfe.xml
vendor/
autoload.php
(more files)
PHP Code
And I am trying a snipped which I found at the library's README on github
<?php
require_once 'vendor/autoload.php';
// var_dump( get_declared_classes() );
echo 'a';
$nfeProc = NFePHPSerialize::xmlToObject(file_get_contents('nfe.xml'));
echo 'b';
//Capturando CNPJ do emitente
$cnpjEmitente = $nfeProc->getNFe()->getInfNFe()->getEmit()->getCNPJ();
echo $cnpjEmitente;
Results
But I am getting the following error:
PHP Fatal error: Class 'NFePHPSerialize' not found in /var/www/html/Test/index.php on line 7
PHP Stack trace:
PHP 1. {main}() /var/www/html/Test/index.php:0
Finally I have uncommented the var_dump( get_declared_classes() ); just to know whether something from the library nfephp-serialize (for tax purposes) is loaded but I found nothing.
Initialization
To init the Test directory, I have issued the following command:
$ composer require jansenfelipe/nfephp-serialize
How to solve?
What is missing is the path associated with the NFePHPSerialize class. It is JansenFelipe\NFePHPSerialize.
You may want to use the class NFePHPSerialize with its namespace, as so:
<?php
require 'vendor/autoload.php';
use JansenFelipe\NFePHPSerialize\NFePHPSerialize;
or simply write:
$nfeProc = JansenFelipe\NFePHPSerialize\NFePHPSerialize::xmlToObject(file_get_contents('nfe.xml'));
How I found the namespace?
You may want to search for the NFePHPSerialize class in the /vendor directory... and find the namespace statement, which is:
namespace JansenFelipe\NFePHPSerialize;
So, the class is available with namespace\class as so:
$var = new JansenFelipe\NFePHPSerialize\NFePHPSerialize(...);
But you can improve this by inserting the use statement as so:
<?php
use JansenFelipe\NFePHPSerialize\NFePHPSerialize;
// some code...
$var = new NFePHPSerialize(...);
I hope this helps you! :)

Using namespaces in PHP

I am working on the AWS documentation which uses Guzzle framework. I have to deal with namespaces here and I am not able to get it working. I went through the docs and examples and understood that we can have packages for projects using namespaces.
I went ahead and tried a simple example, but unsuccessful. Here's the example: this is the index.php:
use My\Full\Classname as Another; //Also tried use My\Full\Classname
$obj = new Another; //with $obj = new Classname;
echo $obj->add();
I have Classname.php in the directory structure like this My->Full->Classname.php:
<?php
class Classname{
public static function add(){
return 2+2;
}
}
?>
I am trying to call the function in index.php but getting error:
Fatal error: Class 'Another' not found in C:\wamp\www\guzzleEx\index.php on line 19
which is the line where I instantiate the Classname object $obj = new Another;
What is the mistake i am making? Is there any INI that needs to be updated or any other config issue? How can I make the code working? If you use the normal include for Classname.php it works fine.
Namespaces need to be explicitly declared, they do not come from a certain directory structure.
So if you do not have a line that reads namespace My\Full; in front of your class Classname, then your class is not in any namespace, but in the root namespace.
Thus you cannot use it as \My\Full\Classname, but \Classname or even Classname directly.

PHP OOP autoloading class namespaces issue

I am trying to upgrade the code of a new project I am working on to comply with PSR-0.
I am using and SPL loader class, However I may be doing something wrong, I just can't spot what the issue is.
I keep getting the following error:
Fatal error: Class 'widezike\General' not found in /nfs/c03/h04/mnt/169128/domains/widezike.com/html/beta/lib/functions.php on line 14
This is my folder structure:
index.php
-lib
config.php
init.php
spl-class-loader.php
functions.php
-widezike
-General.php
This is my functions file where it begins anything to do with the server side code:
<?php
include 'init.php';
include 'config.php';
include 'spl-class-loader.php';
$loader = new SplClassLoader('General', 'lib/widezike');
$loader->register();
use widezike\General;
//Run the output buffer
General::ob();
So this is my code for now, but I can't seem to find what's causing the fatal error...
Thanks in advance
I'm taking a bit of a guess here but I think the issue is in the constructor..
$loader = new SplClassLoader('General', 'lib/widezike');
In the code you linked to they are the namespace and the include path.
Play around with those until it works is all I can suggest.
You might try:
$loader = new SplClassLoader(null, 'lib');
Or
$load = new SplClassLoad('General', 'lib');
On the other hand I personally just use a very simple spl_autoload_register function to do my class loading for me..
spl_autoload_register(function($class){
$filename = str_replace('\\', '/', $class) . '.php';
require($filename);
});
$object = new phutureproof\common\whatever();
I would then have a file /phutureproof/common/whatever.php with the contents:
<?php
namespace phutureproof\common;
class whatever
{
}
I realised that the issue was that my name spaces were wrong, so if you have the same issue just check and make sure that your namespaces are correct!

Abstract a class away from PHP script?

Just wondering if there is anyway to have PHP just run a certain class given the name of the file (controller.php)?
I don't want to require, include or see any signs of it in the controller.php. I just want it to be there.
EDIT: Ok. What I mean by run is in some file hidden away from me I say something like... $class = new Class(); This way I can use $class in my controller.php
ALSO: I'm running PHP 5.3 - So I have namespaces and whatnot.
Anyway of doing this??
Thanks!
Matt Mueller
I'm going to take a big guess at what you really mean. I think you simply want to separate your class PHP file away from your main file without making any obvious includes.
If so, you might want to use the __autoload() function:
<?php
function __autoload($class_name) {
require_once $class_name . '.php';
}
$obj = new MyClass1();
$obj2 = new MyClass2();
?>
This will notice that MyClass1 and MyClass2 haven't yet been defined and will call the autoload function with their class names as the parameter. So then MyClass1.php and MyClass2.php will be require_once'd.
You can autoload the Class file, you will still have to instantiate the Class by hand at some point. Or you will have to include a script that instantiates it for you. Or you re-architect your application so your actual script is included by another script, which pre-instantiates the Class for you.
In short: including the file can be automated, instantiating the Class not so much.

Categories