Use Composer library inside Custom Class - php

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.

Related

get_declared_classes in symfony 3.4

I have app in Symfony 3.4 and I need to get all classes registered in my AppBundle namespace.
I use get_declared_classes(). I've created custom namespace AppBundle\MyCustom and I keep there some classes. The problem is that symfony caches get_declared_classes() and it does not cache my custom namespace. When I use get_declared_classes() for the first time (after remove of cache files) I get all my custom classes, but I am getting a problem when I run script for next time. Any ideas?
here is repo with a problem:
https://github.com/webostin/getdeclaredclasses
The problem is that symfony caches get_declared_classes()
This is a PHP function, Symfony has no control over it.
The problem is might related to autoloading. On the 1st load, Symfony has to build the cache, so includes your class. With a warm cache this is not necessary, so your class is not autoloaded. get_declared_classes wont include your classes.
You can observe this behavior by putting $class = new MyTool(); in the 1st line of your execute method. This will trigger the autoloader, and your class will show up in declared classes.
Solution: It seems you want to get all classes implementing an interface. You can use DI for this.
Create a custom tag, and a manager class, and then use the manager class to access all tagged services in you command.
More here: http://symfony.com/doc/current/service_container/tags.html#creating-custom-tags

How does the PHP 'use' keyword work (in Symfony 2)?

In Symfony 2, all requests are getting routed through app_dev.php (or app.php).
The first few lines look like:
<?php
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Debug\Debug;
...
In theory, I get that this just imports the specified namespace (or class) to the current scope. What I don't understand is how PHP maps this to a file. For example, the Debug class is located in:
vendor/symfony/symfony/src/Symfony/Component/Debug/Debug.php
How does PHP know to look in vendor/symfony/symfony/src/?
I'm probably misunderstanding what is happening, but any clarification would be appreciated.
Knowing which file a class lives in isn't the job of the language, it's the job of the autoloader.
All the use keyword does is this instance is create an alias:
use Symfony\Component\HttpFoundation\Request;
This is saying in the following script, when I refer to Request I really mean Symfony\Component\HttpFoundation\Request. If I use Request object in some way (by either creating an instance or calling a static method on it) then the autoloader will go and try to load the file that class is defined in if it hasn't been loaded already.
The inner workings of the autoloader, though, is different from project to project. There has been a move to standardize autoloader behavior à la PSR-4, but there's nothing in the language saying that you have to adhere to that standard. You could, for instance, have a scheme where all class files reside in a single directory and your autoloader just loads all classes from there, regardless of what namespace they're in.
That scheme would suffer from the fact that you could only have a single class of every name, but there's nothing stopping you from doing it that way.
To answer your question: "How does it know that "Symfony\Component\Debug\Debug" is a valid namespace?"
It doesn't, because it's not actually going out and trying to load that class at this point. If in any random PHP script you put something like this at the top:
use \Non\Existant\ObjectClass;
But don't ever actually use ObjectClass anywhere, you will get no errors. If you try to say new ObjectClass, though, you will get a class not found error.
Simplistically speaking, you have to load all the files beforehand into memory for PHP. PHP does not inherently have any standards to where files are located, the rules for loading files has to be done by your code.
When PHP attempts to load a file, it will check its memory if the class definition exists, if not it will attempt to autoload the file that may contain your class definition; see PHP: Autoloading Classes and spl_autoload_register
Some common autoloading strategies have been defined by the PHP Framework Interop Group:
PSR-0 (Autoloading standard)
PSR-4 (Improved Autoloading standard)
In this case, autoloading is done by Composer, all you need to do is include vendor/autoload.php in any of your scripts and vendor code downloaded via composer can be autoloaded. You can manually add classes into the composer autoloader as well. See: Composer Autoloading

Where do we put non-database functions/library in Laravel 4?

I'm a developer who is just migrating from CodeIgniter to Laravel. In CI I had library folder where I could put non database functions, for example email verification etc. In Laravel I couldn't find something like that ? Where can I put such libraries in Laravel 4?
If it is single PHP file containing different functions then you can create a Model class in your Model folder. But this Class will not extend Eloquent. It is a simple class. For example I have a class as General.php containing static methods of general functionality. As Model is autoloaded so I don't need to worry about including file. It is automatically included.
But if it is a directory containing different PHP files or PHP classes then you can create a directory in your app directory and add the path of that directory in providers array in app.php in config directory. You can also create an aliases of your library by putting entry in aliases under providers in app.php.
Create a library (just a folder), namespace it, and set it in config as a provider.
Then you can call it like
new \Foo\Class()

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.

Include Files in Yii

I am building an application in Yii that used the SwiftMailer Extension. The SwiftMailer examples show that I should include all of the information in the controller file however would it not be more organized and better to put all of the information for the email in a seperate file and include that file?
If yes, what would be the best way to include that file? I am used to just using the include function but I am assuming with Yii it should be its own class under the components folder?
Ya, you probably want to make a custom class and put your SwiftMailer function in there, as well as any related functions, as appropriate. You can either put it in your components directory or a custom "classes" directory depending on how yo want to import it.
In that class, you can include the SwiftMailer libary (see here for an example of how), in your constructor function or similar.

Categories