PHP Flashcards
What does the PSR describe?
The PSR describes a specification for autoloading classes from file paths. It is fully interoperable, and can be used in addition to any other autoloading specification, including PSR-0. This PSR also describes where to place files that will be autoloaded according to the specification.
What does the term “class” refer to?
The term “class” refers to classes, interfaces, traits and other similar structures
Which form does a fully qualified class name have?
\<NamespaceName>(\<SubNamespaceNames>)*\<ClassName>
1. The fully qualified class name MUST have a top-level namespace name, also knows as a “vendor namespace”,
2. MAY have one or more sub-namespace names and
3. MUST have a terminating class name.
4. Underscores have no special meaning in any portion of the fully qualified class name.
5. Alphabetic characters in the fully qualified class name MAY be any combination of lower case and upper case and
6. all class names MUST be referenced in a case-sensitive fashion.
When loading a file that corresponds to a fully qualified class name, a contiguous series of one or more leading namespace and sub-namespace names, not including the leading namespace separator, in the fully qualified class name corresponds to what?
At least one “base directory”
The contiguous sub-namespace names after the “namespace prefix” correspond to what?
They correspond to a subdirectory within a “base directory”, in which the namespace separators represent directory separators. The subdirectory name MUST match the case of the sub-namespace names.
The terminating class name corresponds to a file name ending in which file ending?
.php
The file name MUST match the case of the terminating class name.
What should autoloader implementations not do?
Autoloader implementations MUST NOT throw exceptions, MUST NOT raise errors of and level and SHOULD NOT return a value.
What is composer and what does it do?
Composer is a tool for dependency management in PHP. It allows you to declare the libraries your project depends on and it will manage (install/update) them for you. It enables you to declare the libraries you depend on, finds out which versions of which packages can and need to be installed and installs them.
What differentiates composer from package managers in the sense of Apt?
Both deal with “packages” or libraries, but composer manages them on a per-project basis, installing them in a directory (e.g. vendor) inside your project. By default, it does not install anything globally. Thus, it is a dependency manager. It does however support a “global” project for convenience via the global command.
Which key in your composer.json needs to be specified for the dependencies?
In the composer.json’s require key, you are telling which packages your project depends on:
{ "require": { "example/example": "2.0.*" } }
As you can see, require takes an object that maps package names( e.g. example/example) to version constraints (e.g. 2.0.*).
Composer uses this information to search for the right set of files in package “repositories” that you register using the repositories key, or in Packagist.org, the default package repository. In the above example, since no other repository has been registered in the composer.json file, it is assumed that the example/example package is registered on Packagist.org.
What does a package name consist of?
The package name consists of a vendor name and the project’s name. Often these will be identical, the vendor name only exists to prevent naming clashes. For example, it would allow two different people to create a library names json. One might be names igorw/json while the other might be seldaek/json.
How can you run the initial installation of the defined dependencies for your project?
php composer.phar update
This will make Composer do two things:
1. It resolves all dependencies listed in your composer.json file and writes all of the packages and their exact versions to the composer.lock file, locking the project to those specific versions. You should commit the composer.lock file to your project repo so that all people working on the project are locked to the same versions of dependencies. This is the main role of the update command.
2. It then implictly runs the install command. This will download the dependencies’ files into the vendor directory in your project. (The vendor directory is the conventional location for all third-party code in a project).
What is the point of committing your composer.lock file to version control?
Committing this file to version control is important because it will cause anyone who sets up the project to use the exact same versions of the dependencies that you are using. Your CI server, production machines, other developers in your team, everything and everyone runs on the same dependencies, which mitigates the potential for bugs affecting only some parts of the deployments. Even if you develop alone, in six months when reinstalling the project you can feel confident that the dependencies installed are still working, even if the dependencies have release many new versions since then.
How can you install the dependencies of a composer.lock file?
Running install when a composer.lock file is present resolves and installs all dependencies that you listed in composer.json, but Composer uses the exact versions listed in composer.lock to ensure that the package versions are consistent for everyone working on your project. As a result you will have all dependencies requested by your composer.json file, but they may not all be at the very latest available versions. This is by design, ensuring that your project does not break because of unexpected changes in dependencies.
php composer.phar install
The composer.lock file prevents you from automatically getting the latest versions of your dependencies. How can you circumvent this prevention and get the newest version of one or all dependencies?
php composer.phar update
oder wenn man nur eine dependency updaten möchte:
php composer-phar update example/example
What is a Composer repository? Which repository is the main one used by Composer?
A composer repository is basically a package source: a place where you can get packages from. Packagist.org is the main Composer repository and aims to be the central repository that everybody uses. This means you can automatically require any package that is available there, without further specifying where Composer should look for the package. A library does not need to be on Packagist to be used by Composer, but it enables discovery and adoption by other developers more quickly.
What are platform packages?
They are virtual packages for things that are installed on the system but are not actually installable by Composer. This includes PHP itself, PHP extensions and some system libraries.
For libraries that specify autoload information, which file is used?
Composer generates a vendor/autoload.php file. You can include this file and start using the classes that those libraries provide without any extra work. You can even add your own code to the autoloader by adding an autoload field to composer.json.
As soon as you have a composer.json in a directory, that directory is a what?
It’s a package
When you add a require to a project, you are making a package that depends on other packages. The only difference between your project and a library is what?
Your project is a package without a name