PHP Flashcards

1
Q

What does the PSR describe?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What does the term “class” refer to?

A

The term “class” refers to classes, interfaces, traits and other similar structures

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Which form does a fully qualified class name have?

A

\<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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

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?

A

At least one “base directory”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

The contiguous sub-namespace names after the “namespace prefix” correspond to what?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

The terminating class name corresponds to a file name ending in which file ending?

A

.php
The file name MUST match the case of the terminating class name.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What should autoloader implementations not do?

A

Autoloader implementations MUST NOT throw exceptions, MUST NOT raise errors of and level and SHOULD NOT return a value.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is composer and what does it do?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What differentiates composer from package managers in the sense of Apt?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Which key in your composer.json needs to be specified for the dependencies?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What does a package name consist of?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How can you run the initial installation of the defined dependencies for your project?

A

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).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is the point of committing your composer.lock file to version control?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

How can you install the dependencies of a composer.lock file?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

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?

A

php composer.phar update

oder wenn man nur eine dependency updaten möchte:
php composer-phar update example/example

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is a Composer repository? Which repository is the main one used by Composer?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

What are platform packages?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

For libraries that specify autoload information, which file is used?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

As soon as you have a composer.json in a directory, that directory is a what?

A

It’s a package

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

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?

A

Your project is a package without a name

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

How can you make your package installable?

A

In order to make the package installable you need to give it a name. You do this by adding the name property in composer.json:

{
    "name": "fwani/example",
    "require": {
        "monolog/monolog": "1.0.*"
    }
}
22
Q

In the vast majority of cases, you will be maintaining your library using some sort of version control system like git, svn, hg or fossil. In these cases, Composer infers versions from your VCS and you SHOULD NOT specify a version in your composer.json file. If you are maintaining packages by hand (i.e., without a VCS), how can you specify your version?

A

You’ll need to specify the version explicitly by adding a version value in your composer.json file:

{
    "version": "1.0.0"
}
23
Q

If you do not want to commit the composer.lock file and you are using git, how can you stop it from being committed?

A

By adding it to the .gitignore

24
Q

Which conditions need to be met for your library to be installable?

A

Once you have a VCS (version control system, e.g. git) containing a composer.json file, your library is already composer-installable.

25
Q

What do you need to add to your composer.json to install a library out of a repository that is not on Packagist?

A

Installing something off of github as an example

"repositories": [
    {
        "type": "vcs",
        "url": "https://github.com/fwani/example"
    }
]

Now you can install the dependencies by running Composer’s install command

26
Q

How can you make the installation of your library easier?

A

By putting it on Packagist. It is the default package repository for Composer and everything that is published on Packagist is available automatically through Composer.

27
Q

Which exit codes are possible for Composer commands and what do they mean?

A

0: OK
1: Generic/unknown error code
2: Dependency solving error code

28
Q

How can you add new required packages to the composer.json file from the terminal?

A

php composer.phar require “vendor/package:2.*”

you can specify the vendor, package and version or you can just run the command without it and will be prompted to input everything interactively.

29
Q

How can you remove requirements, that are no longer necessary and have them uninstalled?

A

php composer.phar remove vendor/package vendor/package2

The remove command removes packages from the composer.json file from the current directory. After removing the requirements, the modified requirements will be uninstalled.

30
Q

Which command increases the lower limit of your composer.json version requirements to the currently installed versions?

A

php composer.phar bump

Running this blindly on libraries is NOT recommended as it will narrow down your allowed dependencies, which may cause dependency hell for your users. Running it with –dev-only on libraries may be fine however as dev requirements are local to the library and do not affect consumers of the package.

31
Q

You may want to uninstall and reinstall a package to get a clean installation of it, especially if you messed with the files. How can you get a new clean installation of one or multiple packages?

A

php composer.phar reinstall fwani/example fwani/tired

This would get clean installations for the example and tired package of the vendor fwani, you may also provide a wildcard to reinstall all packages of a certain vendor like this:
php composer.phar reinstall fwani/*

32
Q

How can you search for a package on Packagist?

A

php composer.phar search example

you can also search for more than one term by passing multiple arguments

33
Q

How can you check which packages are in their most up to date version, which packages have updates available and what the currently installed and most up to date versions of your packages are?

A

php composer.phar outdated

The outdated command shows a list of installed packages that have updates available, including their current and latest versions. The color coding is as such:
green (=): Dependency is in the latest version and is up to date.
yellow (~): Dependency has a new version available that includes backwards compatibility breaks according to semver, so upgrade when you can but it may involve work.
red (!): Dependency has a new version that is semver-compatible and you should upgrade it.

34
Q

Which command lets you check which other packages depend on a certain package?

A

php composer.phar depends fwani/example

you can optionally specify a version constraint after the package to limit the search. Add the –tree or -t flag to show a recursive tree of why the package is depended upon, for example:
php composer.phar depends psr/log -t

psr/log 1.1.4 Common interface for logging libraries
├──composer/composer 2.4.x-dev (requires psr/log ^1.0 || ^2.0 || ^3.0)
├──composer/composer dev-main (requires psr/log ^1.0 || ^2.0 || ^3.0)
├──composer/xdebug-handler 3.0.3 (requires psr/log ^1 || ^2 || ^3)
│  ├──composer/composer 2.4.x-dev (requires composer/xdebug-handler ^2.0.2 || ^3.0.3)
│  └──composer/composer dev-main (requires composer/xdebug-handler ^2.0.2 || ^3.0.3)
└──symfony/console v5.4.11 (conflicts psr/log >=3) (circular dependency aborted here)
35
Q

Sometimes certain packages may be blocking a given package from being installed or updated, how can you check which packages may be causing such a blockage and verify whether version upgrades can be performed in your project?

A
php composer.phar prohibits fwani/example 3.1
laravel/framework v5.2.16 requires fwani/tired (2.8.*|3.0.*)

In this example we checked why we can’t update fwani/example to version 3.1 and managed to find out, that the laravel/framework in its currently installed version requires the fwani/tired component of the package to be in version 2.8.* or 3.0.*, which means version 3.1 is too new and officially not supported and that’s what prohibits the updating of fwani/example

36
Q

What should you do to verify that your composer.json file is valid before committing anything?

A

php composer.phar validate

37
Q

What does the config command allow you to do?

A

It allows you to edit Composer config settings and repositories in either the local composer.json file or the global config.json file. Additionally it lets you edit most properties in the local composer.json

38
Q

How can you clear the content from Composer’s cache directories?

A

there are 3 possible commands that all do the same thing:
clear-cache / clearcache / cc

39
Q

If you run into a bug or something is just behaving strangely, which command should you run to perform automated checks for many common problems?

A

php composer.phar diagnose

40
Q

How can you check the packages you have installed for possible security issues?

A

The audit command is used to audit the packages you have installed for possible security issues. It checks for and lists security vulnerability advisories according to the Packagist.org api.
It returns the amount of vulnerabilities found. 0 if successful and up to 255 otherwise.

php composer.phar audit

41
Q

Does Composer see new versions of packages as newer instances of the same package, or as completely separate packages?

A

It’s the latter. Internally, Composer sees every version as a separate package. While this distinction does not matter when you are using Composer, it’s quite important when you want to change it.

42
Q

What is the default timeout in seconds for process executions?

A

300 seconds (5 min.)

43
Q

How can you configure your security audit, so that it ignores certain problems?

A

A list of advisory IDs. remote IDs or CVE IDs that are reported but let the audit command pass,

44
Q

How can you indicate that a type can also be null?

A

By putting a leading question mark symbol (?) in front of it. Nullable types can be formed from any currently permitted type. Nullable types are permitted anywhere type declarations are allowed but are also subject to some inheritance rules. Here is an example to demonstrate basic functionality of a nullable type:

function answer(): ?int {
    return null; //ok
}
45
Q

Parameters with a nullable type do not have a default value. What happens if a value is omitted?

A

If omitted the value does not default to null and will result in an error:

function f(?callable $p) {}
f(); // invalid; function f does not have a default
46
Q

How can you make a parameter both nullable and optional?

A

PHP’s existing semantics allow giving a null default value:

function foo_default(Bar $bar = null) {}
foo_default(new Bar); // valid
foo_default(null); // valid
foo_default(); // valid

As “= null” offers a superset of ?’s functionality, it could be said that “= null” implies ?. However, it is perfectly legal to use both to make a parameter’s nullability explicit:
function foo_both(?Bar $bar = null) {}

47
Q

How can you create a function that does not return anything?

A

By using the void return type. It requires that a function not return any value:

function should_return_nothing(): void {
    return 1; // Fatal error: A void function must not return a value
}

Unlike other return types which are enforced when a function is called, this type is checked at compile-time, which means that an error is produced without the function needing to be called.
A function with a void return type, or void function, may either return implicitly, or have a retuen statement without a value:

function returns_nothing(): void {
    return; // valid
}
48
Q

Can a void function return null?

A

No, a void function may not return any value. Even returning null would result in a fatal error.

49
Q

Can void be used as a parameter type?

A

No, void is only valid as a return type and using it as a parameter type will result in a fatal error.

50
Q

What can iterable be used for?

A

iterable can be used as a parameter type to indicate that a function requires a set of values, but does not care about the form of the value set (array, Iterator, Generator, etc.) since it will be used with foreach. If a value is not an array or instance of Traversable, a TypeError will be thrown.
iterable can also be used as a return type to indicate a function will return an iterable value. If the returned value is not an array or instance of Traversable, a TypeError will be thrown.