I have the APIATO application. And install the library (some wrapper for 3rd part API) by composer.
I need to override classes to add some custom logic into these classes.
Where can I create overridden classes?
There are multiple ways to override the packages
1)Extends the class and override the function.
2)implements the class.
3)You have to create a file in the required container and same way you can use it(same like original usage of the package with overridden file path).
Related
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.
i've worked with older php frameworks such as magento 1.x line where class rewriting (other then by core/community/local overrides) is done using framework config and framework getModel() or getSingleton() like calls, that have the opportunity to check the config before loading the class.
in other words, it could very well be that the config is loading a class under a completely different name not just path.
my question is, how could rewrites like this be supported in a newer psr4/composer aware framework where classes are instantiated directly with the new keyword instead of some getModel() call?
you could perhaps put the config checks directly into the autoloader instead of using a method to get the class, but then what about composer libs, using their own autoloaders like vendor/autoload.php? just chaining the autoloaders would give a hit on the wrong class if the rewrite uses another class name and both exists.
some link or explanation on how to perform both overriding (as in core/community/local) and rewrites (as in config myClassName -> newClassName) using newer namespace, psr4 & composer aware techniques would be most welcomed.
Using composer, I have come across a package that does what I want, if I modify the constant values and some of the code in private methods.
What I've been trying to do is create my own package and extend the other package that I've found and try to override the methods from within my own classes. However, as the package contains constants and private methods, it's difficult/impossible to implement my own package to take advantage of the other package.
What I'm wondering now is what is the best way to go about using the code in this other package? Do I copy the code into my own package and change the namespace and modify the code for my own needs? Or is there another way where I can change the values in another package and modify the private classes?
I'm using Doctrine DBAL to connect to my database. I created a custom class to act as a kind of Controller (it's pretty much just for retrieving data). I'm using composer's autoload to load this class, so I place it in /src/Digital/Data.php (using PSR-0). Everything works fine, but now I need to use Doctrine in /src/Digital/Data.php, do I have to put require 'vendor/autoload.php';in it? In my index file I also have this (to call the Data class). Which is the proper way to use Doctrine in my custom class?
You need to register the ClassLoader only once. The ClassLoader is registered by the vendor/autoload.php file. If you already included that file in the index file, there is no need to do that in any other file.
The best practice is to include it in the frontcontroller or bootstrap file.
I am writing a add-on module which is integrated with an existing PHP application; Because I am using a MVC pattern, and which may requires lot of inclusion of classes (which might not be used at all depending on the action of the user), I decide to use autoloading of classes.
However, I have to ensure that the autoload function does not interferes with the normal operations of the existing applications.
Does autoload only kicks in if a class name is not defined?
Say I have to write another module which uses its own autoload functions (say, I have an autoload for a module, since they each reside in their own folder), how do I differentiate which module is it for?
For #2, I thought of 2 options. Either prefix the class name with the module name (Such as 'MyNewModule_View_Default' and 'AnotherModule_View_Default'), or use file_exists to check the include file exists.
Other suggestions are welcomed too!
Just check if the class that is to be loaded already exists with class_exists() before actually loading it in your autoloader implementation. Especially if you have multiple registered autoloaders (see 2).
You can specify multiple autoloaders in a stack via spl_autoload_register(). The registered functions are executed in the order in which they where registered until the class is successfully loaded. You can specify different autoloaders for different modules for example. Or you can do a namespacing approach like in the Zend_Framework, if you have control over class names.
Yes, autoloader is only called when class name is not found.
Usually you'd check the class' namespace (pre 5.3 you use pseudo-namespaces, usually separated by an underscore). So your autoloader would only load classes that are under the namespace(s) of your application.