Skip to content

Commit ab595c5

Browse files
committed
Add php 8 news
1 parent 1b38ba1 commit ab595c5

File tree

21 files changed

+485
-0
lines changed

21 files changed

+485
-0
lines changed

php8-news/1-nullsafe_operator.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
final class Course {
5+
public function categories(): ?array
6+
{
7+
return null;
8+
}
9+
}
10+
11+
$course = new Course();
12+
13+
// Before
14+
$firstCategoryName = null;
15+
if (null !== $course) {
16+
if (null !== $course->categories()) {
17+
if (null !== $course->categories()->first()) {
18+
$firstCategoryName = $course->categories()->first()->name();
19+
}
20+
}
21+
}
22+
23+
// After
24+
$firstCategoryName = $course?->categories()?->first()?->name();
25+
26+
var_dump($firstCategoryName);

php8-news/10-named_arguments.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
final class Course
4+
{
5+
public function __construct(
6+
private string $id,
7+
private string $name,
8+
private bool $isPublic
9+
) {
10+
}
11+
}
12+
13+
$course = new Course(
14+
"14f5f65c-f6a9-4144-87ad-cf938945b203",
15+
"Novedades PHP 8",
16+
isPublic: false
17+
);
18+
19+
$course2 = new Course(
20+
name: "Novedades PHP 8",
21+
id: "14f5f65c-f6a9-4144-87ad-cf938945b203",
22+
isPublic: false
23+
);
24+
25+
var_dump($course);
26+
var_dump($course2);

php8-news/2-static_return_type.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
abstract class Step {
4+
public static function create(): static {
5+
return new static();
6+
}
7+
}
8+
9+
final class VideoStep extends Step {
10+
}
11+
final class QuizStep extends Step {
12+
}
13+
14+
var_dump(VideoStep::create());
15+
var_dump(QuizStep::create());

php8-news/3-class_object.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace Codely\YouTube\PhpNews;
4+
5+
final class Course {
6+
public function categories(): ?array
7+
{
8+
return null;
9+
}
10+
11+
public static function create(): self
12+
{
13+
return new self();
14+
}
15+
}
16+
17+
$course = new Course();
18+
19+
var_dump(Course::class);
20+
var_dump($course::class);
21+
var_dump((new Course())::class);
22+
var_dump(Course::create()::class);

php8-news/4-union_types.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
final class User {
4+
private float|int $age;
5+
6+
public function __construct(int|float $age)
7+
{
8+
$this->age = $age;
9+
}
10+
11+
}
12+
13+
var_dump(new User(18));
14+
var_dump(new User(18.5));

php8-news/5-throw_expression.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
$user = null;
4+
5+
6+
// Before
7+
if (null !== $user) {
8+
echo $user;
9+
} else {
10+
throw new Exception();
11+
}
12+
13+
// After
14+
echo $user ?? throw new Exception();

php8-news/6-empty_catch.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
$user = null;
4+
5+
try {
6+
echo $user ?? throw new Exception();
7+
} catch (Exception) {
8+
echo "Some error just happened";
9+
}

php8-news/7-attributes.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
use Symfony\Component\Routing\Annotation\Route;
4+
5+
// Before
6+
final class CoursePostController
7+
{
8+
/**
9+
* @Route("/courses", "create_course")
10+
*/
11+
public function create(): Response
12+
{
13+
// Controller stuff
14+
}
15+
}
16+
17+
// After
18+
final class NewCoursePostController
19+
{
20+
#[Route('/courses', 'create_course')]
21+
public function create(): Response
22+
{
23+
// Controller stuff
24+
}
25+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Symfony\Component\Routing\Annotation\Route;
6+
7+
class SomeController
8+
{
9+
#[Route('/path', name: 'action')]
10+
public function someAction()
11+
{
12+
// ...
13+
}
14+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Symfony\Contracts\Service\Attribute\Required;
6+
7+
class SomeService
8+
{
9+
#[Required]
10+
public Bar $bar;
11+
12+
#[Required]
13+
public function setFoo(Foo $foo): void
14+
{
15+
// ...
16+
}
17+
}

0 commit comments

Comments
 (0)