Can I include a non-Symfony class? - php

I'm actually learning Symfony and I don't understand the fact of including classes. Can I include a standalone class - this means, not designed for a framework - and then use it - let's say, Amazon S3 class -?

Just put your class in wherever lib (sub)directory it suits you the best. It will be autoloaded by the framework.
If you don't want it to be automatically autoloaded than put it somewhere in the lib/vendor directory.
If your class is reusable than it's the best to put it in a plugin.

Related

PHP: How to extend a 3rd party vendor class downloaded with composer and load it instead of the original?

I have a library downloaded with Composer which performs several basic stuff but which has very poor class methods in some cases.
So I though to create my own class (for certain objects, not all of them) which extend the library base class and add some useful method to it.
Unfortunately I'm not very familiar with Composer and autoloader.php.
How can I achieve that? I need to create my own library and run composer?
Two choices come to my mind, you can:
Fork the entire project and change as many things as you want
Extend the classes you need, and replace declarations/injections in your src code, there's no need to modify the autoloader.

Where should I add my internal classes file in symfony2?

I'm porting an old project to symfony2 in order to start learning the framework and I cannot realize how to properly add some helper functions I have in a PHP file.
Services. Wrap those function in a class (like Helper), define the class as a service and then inject it where needed (controller or another service, or even into a cli command).
The namespace (thus the path, as you tagged the question with psr-0) is up to you.

PHPUnit Path problems

I am writing a ZF application. I had some abstract parent classes in a library directory. Netbeans could generate test skeletons for the child classes no problems, and I could run the tests no problem.
Later on I decided to move the abstract parents out of the library and in to the application directory (to improve readability). I updated my application code accordingly, and it runs no problem.
However, now when I use netbeans to generate test skeletons for the child classes, it gives a fatal error saying it can not find the parent. I then constructed a test class manually and ran it from the command line, and PHPUnit gave the same error.
What do I need to do to get this pathing working correctly? In the PHPUnit bootstrap I tried adding the Application directory to the include path, and registering Application as a namespace with the Zend Autoloader. I don't think this is a recommended practice, and it failed anyhow...
I really do struggle with path issues, finding files, etc...
Any assistance is much appreciated.
OK, so I can move the classes from the library to the core code directories, and the core code finds them no problems because they conform to the standard naming conventions autoladed by ZF.
Other applications (e.g. PHPUnit) won't be able to find them though. I could get around this by setting their autoloading the same ZF. Alternatively, I could incorporate required files with require_once (which would negate the use of autoloading in the first place).
So, it seems that my best shot is to put the shared classes back in the library. My original PHPUnit bootstrap adds the library prefix to the ZF autoloader, so I'm good to go.
All-in-all, I've just learned another good use for libraries.
[EDIT:] Another Alternative...
My test classes are boostrapping Zend Application which sets up the autoloading for PHPUnit. The only drawback doing it this way is that PHPUnit (via my netbeans IDE) can't create the skeleton tests for me. I guess I'll have to live with that.

include on instantiate PHP

Well I have a question. On Zend Framework We use a very interesting structure because if you intanciate a class then automatically Zend include the class file and after instanciate it (Class obviously allow to ge the directory's structure Class: abc_def_pqr then path is abc/def/pgr.php)
Does anyone have any idea???
Zend Framework uses spl_autoload_register to register a function that is called when a class is called but does not exist.
The function then does as you say, replace the underscores with directory separators and tries to include the file.
See Autoloading Classes but note using spl_autoload_register instead of __autoload allows for multiple autoload functions, which is better practice. Especially if being used in conjunction with ZF or external libraries.

Using third party code within Zend Framework

I want to use a small library within the Zend Framework (simple_php_dom, for what it's worth).
Should I just stick it in library/, include it where I want to use it (like in a specific controller) like include('library/foo.php'); and have at it?
If not, how should I do it? What's the "Zend Framework" way of doing something like this?
Since the library doesnt support PEAR conventions its not really easy to hook it up to the autoloader, so i would just manually require_once it in the controller or model that uses it. If it was used extensively I might make a wrapper class to proxy calls through and autoload that (that class having the require_once).

Categories