PHP 8.0 contains many new features and optimizations including named arguments, union types, attributes, constructor property promotion, match expression, nullsafe operator, JIT, and improvements in the type system, error handling, and consistency.
New Features 8.0 var_dump (array_fill (value : 50 , count : 3 , start_index : 0 ));
#[\Attribute ] class JsonSerialize { public function __construct (public ?string $fieldName = null ) { var_dump ("JsonSerialize:$fieldName " ); } } class VersionedObject { #[JsonSerialize ('json-foobar' )] protected string $myValue = '' ; } $object = new VersionedObject ();$reflection = new ReflectionObject ($object );foreach ($reflection ->getProperties () as $reflectionProp ) { foreach ($reflectionProp ->getAttributes (JsonSerialize ::class ) as $jsonSerializeAttr ) { var_dump ($jsonSerializeAttr ->getName ()); var_dump ($jsonSerializeAttr ->getArguments ()); var_dump ($jsonSerializeAttr ->getTarget ()); var_dump ($jsonSerializeAttr ->isRepeated ()); var_dump ($jsonSerializeAttr ->newInstance ()); } }
class Point { public function __construct (protected int $x , protected int $y = 0 ) {} } var_dump (new Point (2 ,4 ));class Point2 { protected int $x ; protected int $y ; public function __construct (protected int $x , protected int $y = 0 ) {} }
public function parse_value (string |int |float ): string |null {}
echo match (8.0 ) { '8.0' => "Oh no!" , 8.0 => "This matches" , };
$result = $repository ?->getUser (5 )?->name;if (is_null ($repository )) { $result = null ; } else { $user = $repository ->getUser (5 ); if (is_null ($user )) { $result = null ; } else { $result = $user ->name; } }
echo str_replace (...[ 'replace' => 'Iron Man' , 'search' => 'Inevitable' , 'subject' => 'I am Inevitable' , ]);
function test ($a , $b , ...$args ) { var_dump ($args ); } test (28 , 15 , june : 6 , september : 15 , january : "01" );
<?php function fibonacci ($n ) { if ($n < 2 ) return $n ; return fibonacci ($n - 1 ) + fibonacci ($n - 2 ); } $start = microtime (true );fibonacci (42 );$end = microtime (true );echo "Time taken: " . ($end - $start ) . " seconds\n" ;
$wm = new WeakMap ();$o = new StdClass ;class A { public function __destruct ( ) { echo "Dead!\n" ; } } $wm [$o ] = new A;var_dump (count ($wm ));unset ($o );var_dump (count ($wm ));$wm = new SplObjectStorage ();$o = new StdClass ;class A { public function __destruct ( ) { echo "Dead!\n" ; } } $wm [$o ] = new A;var_dump (count ($wm ));unset ($o );var_dump (count ($wm ));
class A { public function method (int $many , string $parameters , $here ) {} } class B extends A { public function method (...$everything ) {} } class C extends A { public function method (int ...$everything ) {} }
class Test { public function create ( ): static { return new static (); } }
class Test {}$obj = new Test ;var_dump ($obj ::class );var_dump (get_class ($obj ));
class A {};class B {};new (1 >2 ? "A" : "B" )();
trait T { abstract private function a ( ) ; }
$value = $config ['path' ] ?? throw new InvalidArgumentException ('path required' );$fn = fn ( ) => throw new Exception ('Exception in arrow function' );
function functionWithLongSignature ( Type1 $parameter1 , Type2 $parameter2 , // <-- This comma is now allowed. ) {}
try {}catch (Exception ) {}
class ParentClass { private function method1 ( ) {} private function method2 ( ) {} private static function method3 ( ) {} } abstract class ChildClass extends ParentClass { public abstract function method1 ( ) ; public static function method2 ( ) {} public function method3 ( ) {} }
str_contains ('Foobar' , 'Foo' ); str_starts_with ('PHP 8' , 'PHP' ); str_ends_with ('Version8' , '8' );
Backward Incompatible Changes 8.0 var_dump (0 == "foo" );var_dump (42 == "42foo" );var_dump (0 == "" );var_dump ("" < 0 );var_dump ("" < -1 );var_dump ("a" < 0 );var_dump ("b" < -1 );var_dump ("ab" > 0 );
class Test { public function method1 ( ) {} } var_dump (is_callable ([Test ::class , 'method1' ]));var_dump (is_callable ([new Test , 'method1' ]));
– EOF –