“Lambda functions” is the technical term for “anonymous functions”, i.e. functions that are not named (function foo(a) { }, called foo(b)), but may still be assigned to variables ($foo = function (a) { };, called $foo(b);).
array_filter is one of many useful functions included with PHP.
A router maps the URI to the corresponding controller
parse_url() splits a URI into path and query(string)
array_key_exists() checks if an array has a given key
http_response_code() sets the HTTP status code
Remember: For variables to be substituted within strings, the string must be enclosed in double quotes, and the variable must be enclosed in (a single set of) curly brackets
Set a default for a parameter to make it optional, e.g.:
phpenmod, phpdismod and phpquery are tools for enabling, disabling and viewing the status of PHP modules, they are included in the php-common package (on Ubuntu 22.04)
Creating a MySQL user for the demo/course app:
CREATE USER 'myapp'@'localhost' IDENTIFIED BY 'SOME_SUPER_SECURE_PASSWORD_GOES_HERE';GRANT ALL ON myapp.* TO 'myapp'@'localhost';FLUSH PRIVILEGES;
For the demo to work, I had to change the host in the DSN from “localhost” to “127.0.0.1”.
To avoid SQL injection, substitute any dynamic (user-provided) values in SQL statements with ? or :name placeholders, and bind the actual values by providing them as an array to the execute() method
Unique indices prevent multiple rows from having the same value in the same column (e.g. multiple users having the same email address)
Foreign keys reference other tables
ON UPDATE/ON DELETE configure rules to keep the database integrity intact, e.g. here to automatically delete all of a user’s notes when the user gets deleted
Use client-side validation to provide faster feedback to the regular user, but server-side validation to avoid abuse by malicious actors (or users with Javascript disabled for some reason)
isset() can be used to determine if a variable exists (including whether a key exists in an associate array) and is set to something else than null
The “null coalescing operator” ??, introduced in PHP 7, uses the following value if the preceding one does not exist, e.g. the following snippets are equivalent:
trim() removes whitespace from the beginning and end of a string
INF is a global constant for “infinity”, i.e. the largest number
A “pure function” does not require any state from the outside (“self-contained”; only relying on its input to produce its output)
Methods which are pure functions can be made “static”, which means they can be called directly using the class and the :: operator, without having to instantiate an object of the class first
filter_var() can e.g. be used for validating that something looks like a valid email address
The path to the project root is often called BASE_PATH.
Constants are declared using the const keyword and do not start with a $ sign.
extract() turns an array into a set of (declared) variables
spl_autoload_register() can be used to autoload classes and avoid the issue of having to require them (and the risk of requiring them twice, resulting in an error about duplicate declaration)
The namespace keyword is used to declare a namespace in PHP
Namespaces are used to prevent conflicts between classes that have the same name and can be used to organize code by grouping related classes together
When using a namespaced file, use the use keyword to “import” it
str_replace() replaces occurances of strings in strings with other strings
DIRECTORY_SEPARATOR is a global constant that contains the current operating system’s directory separator (e.g., / or \)
By now it should be obvious that the convention is to name constants with all capital letters 😀
Every class mentioned in a “namespaced” class is assumed to be in the same namespace
To reference a class in the “global” namespace (i.e. without a namespace), prefix it with the \ (backslash) character
Instead, one can also use a class from the global namespace, which also serves as a sort of documentation (answering the question “which other classes are used within this class?“)
To store something, make a POST request to the plural, e.g. POST /notes
To delete something, use the HTTP DELETE method, or if that is not feasible (e.g. because of not wanting to depend on JavaScript), use the HTTP POST method and a hidden form field called e.g. “_method” that provides the intended HTTP method (e.g. “DELETE”)
Laravel
To store something, call the controller action “store”
To delete something, call the controller action “destroy”
Place the “happy path” (execution without errors) at the bottom of the code
The purpose of this lesson is to understand the concepts behind service containers. In the real world, a framework like Symphony or Laravel would provide this functionality.
“bind” ⇒ “add something to the container”
Binds a key (string) to a factory function (builder, resolver)
“resolve” ⇒ “get something from the container”
call_user_func is a method that calls a function
To throw an exception, use the throw keyword, e.g.
throw new \Exception();
Static functions can be called without instantiating an object of a class
A class has a static property ::class that returns its fully namespaced name
In addition to the naive container built in this lesson, Laravel service containers support singletons (get the same instance no matter how many times you resolve it) and automatic dependency resolution.
In a static method, the keyword static refers to the class.
The keyword extends is used to inherit from a parent class. (Also, implements indicates that a class implements an interface.)
Exceptions can be thrown with the throw statement and caught with try/catch.
In PHP 8, by prefixing the constructor’s arguments with a visibility (e.g. public/protected), they no longer need to be assigned to instance attributes manually (see Constructor Property Promotion):
public function __construct(public array $attributes){ // ...}
The readonly keyword means that a variable can only be assigned to once.
Delegate instead of micromanaging (move functionality out of the controller)