\documentclass[10pt,a4paper]{article} % Packages \usepackage{fancyhdr} % For header and footer \usepackage{multicol} % Allows multicols in tables \usepackage{tabularx} % Intelligent column widths \usepackage{tabulary} % Used in header and footer \usepackage{hhline} % Border under tables \usepackage{graphicx} % For images \usepackage{xcolor} % For hex colours %\usepackage[utf8x]{inputenc} % For unicode character support \usepackage[T1]{fontenc} % Without this we get weird character replacements \usepackage{colortbl} % For coloured tables \usepackage{setspace} % For line height \usepackage{lastpage} % Needed for total page number \usepackage{seqsplit} % Splits long words. %\usepackage{opensans} % Can't make this work so far. Shame. Would be lovely. \usepackage[normalem]{ulem} % For underlining links % Most of the following are not required for the majority % of cheat sheets but are needed for some symbol support. \usepackage{amsmath} % Symbols \usepackage{MnSymbol} % Symbols \usepackage{wasysym} % Symbols %\usepackage[english,german,french,spanish,italian]{babel} % Languages % Document Info \author{Dave Child (DaveChild)} \pdfinfo{ /Title (php8-what-s-new-in-php8.pdf) /Creator (Cheatography) /Author (Dave Child (DaveChild)) /Subject (PHP8 - What's New In PHP8? Cheat Sheet) } % Lengths and widths \addtolength{\textwidth}{6cm} \addtolength{\textheight}{-1cm} \addtolength{\hoffset}{-3cm} \addtolength{\voffset}{-2cm} \setlength{\tabcolsep}{0.2cm} % Space between columns \setlength{\headsep}{-12pt} % Reduce space between header and content \setlength{\headheight}{85pt} % If less, LaTeX automatically increases it \renewcommand{\footrulewidth}{0pt} % Remove footer line \renewcommand{\headrulewidth}{0pt} % Remove header line \renewcommand{\seqinsert}{\ifmmode\allowbreak\else\-\fi} % Hyphens in seqsplit % This two commands together give roughly % the right line height in the tables \renewcommand{\arraystretch}{1.3} \onehalfspacing % Commands \newcommand{\SetRowColor}[1]{\noalign{\gdef\RowColorName{#1}}\rowcolor{\RowColorName}} % Shortcut for row colour \newcommand{\mymulticolumn}[3]{\multicolumn{#1}{>{\columncolor{\RowColorName}}#2}{#3}} % For coloured multi-cols \newcolumntype{x}[1]{>{\raggedright}p{#1}} % New column types for ragged-right paragraph columns \newcommand{\tn}{\tabularnewline} % Required as custom column type in use % Font and Colours \definecolor{HeadBackground}{HTML}{333333} \definecolor{FootBackground}{HTML}{666666} \definecolor{TextColor}{HTML}{333333} \definecolor{DarkBackground}{HTML}{5A68A5} \definecolor{LightBackground}{HTML}{F4F5F9} \renewcommand{\familydefault}{\sfdefault} \color{TextColor} % Header and Footer \pagestyle{fancy} \fancyhead{} % Set header to blank \fancyfoot{} % Set footer to blank \fancyhead[L]{ \noindent \begin{multicols}{3} \begin{tabulary}{5.8cm}{C} \SetRowColor{DarkBackground} \vspace{-7pt} {\parbox{\dimexpr\textwidth-2\fboxsep\relax}{\noindent \hspace*{-6pt}\includegraphics[width=5.8cm]{/web/www.cheatography.com/public/images/cheatography_logo.pdf}} } \end{tabulary} \columnbreak \begin{tabulary}{11cm}{L} \vspace{-2pt}\large{\bf{\textcolor{DarkBackground}{\textrm{PHP8 - What's New In PHP8? Cheat Sheet}}}} \\ \normalsize{by \textcolor{DarkBackground}{Dave Child (DaveChild)} via \textcolor{DarkBackground}{\uline{cheatography.com/1/cs/27364/}}} \end{tabulary} \end{multicols}} \fancyfoot[L]{ \footnotesize \noindent \begin{multicols}{3} \begin{tabulary}{5.8cm}{LL} \SetRowColor{FootBackground} \mymulticolumn{2}{p{5.377cm}}{\bf\textcolor{white}{Cheatographer}} \\ \vspace{-2pt}Dave Child (DaveChild) \\ \uline{cheatography.com/davechild} \\ \uline{\seqsplit{aloneonahill}.com} \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Cheat Sheet}} \\ \vspace{-2pt}Published 31st March, 2021.\\ Updated 31st March, 2021.\\ Page {\thepage} of \pageref{LastPage}. \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Sponsor}} \\ \SetRowColor{white} \vspace{-5pt} %\includegraphics[width=48px,height=48px]{dave.jpeg} Measure your website readability!\\ www.readability-score.com \end{tabulary} \end{multicols}} \begin{document} \raggedright \raggedcolumns % Set font size to small. Switch to any value % from this page to resize cheat sheet text: % www.emerson.emory.edu/services/latex/latex_169.html \footnotesize % Small font. \begin{multicols*}{3} \begin{tabularx}{5.377cm}{x{1.64241 cm} x{3.33459 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{PHP8 Variable Types}} \tn % Row 0 \SetRowColor{LightBackground} \seqsplit{Class/interface} name & An instance of the class or interface \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} self & The same class the type is used in \tn % Row Count 4 (+ 2) % Row 2 \SetRowColor{LightBackground} parent & The parent of the class the type is used in \tn % Row Count 6 (+ 2) % Row 3 \SetRowColor{white} callable & Something callable \tn % Row Count 7 (+ 1) % Row 4 \SetRowColor{LightBackground} bool & A boolean \tn % Row Count 8 (+ 1) % Row 5 \SetRowColor{white} int & An integer \tn % Row Count 9 (+ 1) % Row 6 \SetRowColor{LightBackground} float & A float \tn % Row Count 10 (+ 1) % Row 7 \SetRowColor{white} string & A string \tn % Row Count 11 (+ 1) % Row 8 \SetRowColor{LightBackground} array & An array \tn % Row Count 12 (+ 1) % Row 9 \SetRowColor{white} iterable & Array or traversable \tn % Row Count 13 (+ 1) % Row 10 \SetRowColor{LightBackground} object & An object \tn % Row Count 14 (+ 1) % Row 11 \SetRowColor{white} mixed & Any value \tn % Row Count 15 (+ 1) % Row 12 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{{\bf{Return Types}}} \tn % Row Count 16 (+ 1) % Row 13 \SetRowColor{white} void & Function will not return anything \tn % Row Count 18 (+ 2) % Row 14 \SetRowColor{LightBackground} static & Function may return `static` \tn % Row Count 20 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{p{0.4177 cm} p{1.37841 cm} p{0.91894 cm} x{1.46195 cm} } \SetRowColor{DarkBackground} \mymulticolumn{4}{x{5.377cm}}{\bf\textcolor{white}{New `mixed` type}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{4}{x{5.377cm}}{Mixed means any one of:} \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} & array & bool & callable \tn % Row Count 2 (+ 1) % Row 2 \SetRowColor{LightBackground} & string & int & resource \tn % Row Count 3 (+ 1) % Row 3 \SetRowColor{white} & float & null & object \tn % Row Count 4 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}----} \SetRowColor{LightBackground} \mymulticolumn{4}{x{5.377cm}}{Note that because `mixed` already contains `null`, it is not nullable itself.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}----} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Return static}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{class Foo \newline \{ \newline public function test(): static \newline \{ \newline return new static(); \newline \} \newline \}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{Static is now a valid return type.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{The nullsafe operator (copy)}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{\$foo = \$bar-\textgreater{}getBaz()?-\textgreater{}asFormatted();} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{Quickly check for nulls in method returns.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Union types}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{public function foo(Foo|Bar \$input): int|float;} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{You can now specify more than one type in a type hint: \newline • The `void` type cannot be part of a union. \newline • Unions can be nullable by including `null` as a type or preceding with a question mark.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Named arguments}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{function foo(string \$a, int \$b, ?string \$c = null) \newline \{ \newline ... \newline \} \newline \newline foo( \newline c: 'value c', \newline a: 'value a' \newline );} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{Pass values to functions and methods by name, regardless of the order in which they are defined.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Attributes}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{use App\textbackslash{}Attributes\textbackslash{}ExampleAttribute; \newline \newline \#{[}ExampleAttribute{]} \newline class Foo \newline \{ \newline \#{[}ExampleAttribute{]} \newline public const FOO = 'foo'; \newline \newline \#{[}ExampleAttribute{]} \newline public \$x; \newline \newline \#{[}ExampleAttribute{]} \newline public function foo(\#{[}ExampleAttribute{]} \$bar) \{ \} \newline \}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{Structured metadata} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Match expression}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{\$result = match(\$input) \{ \newline 0 =\textgreater{} "hello", \newline '1', '2', '3' =\textgreater{} "world", \newline \};} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{Switch statements made simple. Note, the comparisons made by `match` use strict types.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Constructor property promotion}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{class Money \newline \{ \newline public function \_\_construct( \newline public Currency \$currency, \newline public int \$amount, \newline ) \{\} \newline \}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{In the above example `Currency` and `int` are both automatically made public properties of the class and assigned values on construction.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Weak maps}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{class Foo \newline \{ \newline private WeakMap \$cache; \newline \newline public function \seqsplit{getSomethingWithCaching(object} \$obj): object \newline \{ \newline return \$this-\textgreater{}cache{[}\$obj{]} \newline ??= \$this-\textgreater{}computeSomethingExpensive(\$obj); \newline \} \newline \}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{A weak map allows for storage of key-value pairs, but the PHP garbage collection will drop a weak map element where the only reference to it is the weak map itself.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Throw expression}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{\$triggerError = fn () =\textgreater{} throw new MyError(); \newline \$foo = \$bar{[}'offset'{]} ?? throw new CustomEx('offset');} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{Throw is now an expression instead of a statement, making it more flexible.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Non-capturing catches}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{try \{ \newline // Something goes wrong \newline \} catch (MySpecialException) \{ \newline Log::error("Something went wrong"); \newline \}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{When you catch exceptions and throwables, you no longer need a variable.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{::class on objects}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{\$foo = new Foo(); \newline var\_dump(\$foo::class);} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{Works the same as `get\_class()`.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Trailing comma in parameter lists}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{public function( \newline string \$parameterA, \newline int \$parameterB, \newline Foo \$objectfoo, \newline ) \{ \newline // … \newline \}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{You can now end a parameter list with a comma without an error.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Create DateTime objects from interfaces}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{\seqsplit{DateTime::createFromInterface(DateTimeInterface} \$other); \newline DateTimeImmutable::createFromInterface(DateTimeInterface \$other);} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{New Stringable interface}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{class Foo \newline \{ \newline public function \_\_toString(): string \newline \{ \newline return 'foo'; \newline \} \newline \} \newline \newline function bar(string|Stringable \$stringable) \{ /{\emph{ … }}/ \} \newline \newline bar(new Foo()); \newline bar('abc');} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{2.43873 cm} x{2.53827 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{New functions}} \tn % Row 0 \SetRowColor{LightBackground} \seqsplit{`str\_contains('haystack'}, 'needle')` & Does haystack contain needle? \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} \seqsplit{`str\_starts\_with('haystack'}, 'needle')` & Does haystack start with needle? \tn % Row Count 5 (+ 3) % Row 2 \SetRowColor{LightBackground} \seqsplit{`str\_ends\_with('haystack'}, 'needle')` & Does haystack end with needle? \tn % Row Count 7 (+ 2) % Row 3 \SetRowColor{white} `fdiv()` & Handles division by zero without an error. \tn % Row Count 10 (+ 3) % Row 4 \SetRowColor{LightBackground} `get\_debug\_type()` & Like `gettype()` but with more detail \tn % Row Count 12 (+ 2) % Row 5 \SetRowColor{white} \seqsplit{`get\_resource\_id()`} & Fetch the numeric ID of a resource \tn % Row Count 14 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Breaking Changes}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{Internal errors and engine warnings have been made consistent and/or reclassified.} \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{The `@` operator no longer silences fatal errors.} \tn % Row Count 3 (+ 1) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{The default error reporting level is now `E\_ALL`.} \tn % Row Count 4 (+ 1) % Row 3 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{The default PDO error mode is no longer silent.} \tn % Row Count 5 (+ 1) % Row 4 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{Concatenation precedence has changed - addition now precedes concatenation.} \tn % Row Count 7 (+ 2) % Row 5 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{Namespaces can now include reserved names.} \tn % Row Count 8 (+ 1) % Row 6 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{\seqsplit{`ReflectionFunction::isDisabled()`}, \seqsplit{`ReflectionParameter::getClass()`} and \seqsplit{`ReflectionParameter::isCallable()`} have been deprecated.} \tn % Row Count 11 (+ 3) \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{There are more breaking and engine changes, this list is limited to those you are most likely to encounter. For a full run-down, check out Brent's article linked below.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{With Thanks}} \tn \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{This is a condensed version of the excellent PHP8 write-up you can find by \{\{link="https://stitcher.io/blog/new-in-php-8"\}\}Brent at stitcher.io\{\{/link\}\}% Row Count 4 (+ 4) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} % That's all folks \end{multicols*} \end{document}