I am using Codeigniter for a project and I want to incorporate a library into the project, but it is setup with various files using namespaces and the 'use' keyword to include the required files. How do I convert this to work with Codeigniter? Do I just replace every use LibName\Something\Something with a require() statement, or do I do something different?
I'd like top use this library in CI3 unless there's a better way:
https://github.com/webonyx/graphql-php
You will need the use statement in order to use the library. So, don't go hacking graphql-php.
Since the library uses Composer it's very easy to put that autoloader to work with CodeIgniter. In /application/config/config.php set the following "Composer auto-loading" section
$config['composer_autoload'] = TRUE;
Read the comments in config.php if you have changed the location of the "vendor" folder from the usual spot.
After that, you use the standard PHP syntax to use and instantiate classes in the library.
You should probably avoid namespacing your application, meaning anything using CodeIgniter's core classes. That approach has worked for me when combining libraries like graphql-php (e.g. PhpSpreadSheet) and CodeIgniter.
Related
In my project I have some features that can be extended by end user on production server by just uploading a class file into a specific directory (like Wordpress plugins directory) so this classes can be added and removed dynamically at any time.
Currently I'm doing this using spl_autoload_register function.
Can I make this functionality available using Composer, in order to make the project more standard?
The answer is probably yes, if you stick to PSR-0 or (preferred) PSR-4 standard with your class and file names.
I know this is highly unlikely but thought I would ask anyway.
I am using DomPdf to render pdfs.
Currently it does not implement namespaces and loads a lot of classes.
I can autoload the library with composer and "classmap": ["include/"].
Is it in any way possible to autoload this library implementing a custom namespace in order to avoid possible class name conflicts?
Or is there a tool to search and replace in a library to add namespaces?
I would prefer not to touch the library and was just wondering if there is some way this could be done with composer's autoloader.
(I don't currently have conflicts but would like to keep my libs from running into future issues by implementing namespaces wherever I can.)
Conflicts would arise because of two classes having the same name, the same (or absent) namespace, but reside in different files (and paths) and contain different code.
Composer autoloading cannot do anything about this.
If you come into this situation, you have to resolve it on the code level first, i.e. you have to rename one of the classes, probably moving it into a namespace and keeping it's name as a quick help. Effectively renaming it means to fix every other line of code that has references to the old class.
Fortunately this will only happen if you try to include new code into your project, so you'd be able to stop using whatever you started importing, and look around for an alternative instead.
I played with the zf2-tutorial successfully, but I was totally confused when trying to integrate an external library like "jpgraph". I know I must do this with autoload or servicemanager but it won't work.
The php-files of jpgraph are in the vendor/graph directory. I use a module called Jpgraph, in the controller indexAction I try:
$graph = new Graph($width,$height);
this gives me an error:
Fatal error: Class 'Jpgraph\Controller\Graph' not found in ...
the jpgraph library don't use namespaces.
i also tried this way without success
what's the best way to integrate such things?
I would be glad for every tip or help
Add the library to your composer.json and add the class with Classmap and/or the include path as phpunit does
https://github.com/sebastianbergmann/phpunit/blob/master/composer.json#L48
One option, as Maks3w pointed out, is to use Composer. If you've never heard of or used composer before it's definitely worth a look. I was surprised how easy it was to set up and use 3rd party libraries. It's also very easy to set up your own library to work with composer, and use any source controlled (git or svn) library of your own - works well with GitHub repos - just add a composer.json file.
On the other hand, you do not need to use composer to do what you want, it would make it very easy, but it may be overkill. Zend Framework 2 has a very flexible autoloader system, and although it works well with PSR-0, you can have any class autoloading sytem that you like. Take a look at the different components of Zend\Loader, in particular I think the ClassMapAutoloader will be the one to suit your needs.
I am creating a PHP library of the functions and snippets I use most.
I am trying to set the library project up so that I can use it across other projects.
Currently I have it in the global include folder and the autocomplete for my functions is working well.
However when I run the functions I receive an error because they are not included how do I go about finding them to include them. Or is there a way for me to auto copy them into the source folder of the second project.
Add path to your library into include_path in your php.ini
I'm going to use Zend framework but just some tool of Zend like translate, date and cache. Can I use it as standalone class? My project has it own structure and I don't want use the whole Zend fw. If yes, which files should I include in my project? Is there a docs for using each Zend fw tool as standalone?
And remember, to use various Zend Framework components in another project, you just need to have the Zend library somewhere on your include_path. Copying the whole thing may seem overkill to use one component, but it's only disk space. Having those files there doesn't affect performance unless they are called upon. And this way, you don't have to sweat the dependencies, like Zend_Exception and its various component-specific subclasses.
So, for example, if you have a folder myapp/lib to contain your external libraries, you simply make sure that your include path contains that lib folder and copy the Zend folder into it as myapp/lib/Zend.
Then to use a component like Zend_Translate, all you have to do is something like the following:
require_once 'Zend/Translate.php';
$options = array(
// your options here
);
$translate = new Zend_Translate($options);
With some kind of autloading mechanism in place, you can avoid even the require_once call. Setting up autoloading is as easy as putting the following in some kind of common/bootstrap file:
require_once 'Zend/Loader/Autoloader.php';
Zend_Loader_Autoloader::getInstance();
Then any classes that follow the PEAR 1-class-1-file naming convention can be loaded without explicitly adding any require/include statements.
If disk-space really is a concern and you really don't want the whole Zend library, then you could investigate a packageizer, like Jani Hartikainen's Packageizer.
As an answer i could say Yes of course.
for example if u want to use Zend_Translate copy Translate.php and Translate folder to your library directory.
some times inside a class some other classes have been used. u have to copy them too. i find them by reading raised errors. ;)