The essential news about content management systems and mobile technology. Powered by Perfect Publisher and XT Search for Algolia.
The News Site publishes posts to the following channels: Facebook, Instagram, Twitter, Telegram, Web Push, Tumblr, and Blogger.
The Property Hooks RFC passed a significant milestone, getting an overwhelmingly positive 34 "yes" votes and only 1 "no" vote. That's well above the required 2/3 majority required to pass.
What are property hooks in PHP?
Here's the proposal summary from the RFC:
Developers often use methods to wrap and guard access to object properties. There are several highly common patterns for such logic, which in practice may be verbose to implement repeatedly. Alternatively, developers may use __get and __set to intercept reads and writes generically, but that is a sledge-hammer approach that intercepts all undefined (and some defined) properties unconditionally. Property hooks provide a more targeted, purpose-built tool for common property interactions...
This RFC introduces two “hooks” to override the default “get” and “set” behavior of a property. Although not included in this initial version, the design includes the ability to support more hooks in the future.
Property hooks are inspired by languages like Kotlin, C#, and Swift, and the syntax includes two syntax variants that resemble short and multi-line closures:
class User implements Named
{
private bool $isModified = false;
public function __construct(
private string $first,
private string $last
) {}
public string $fullName {
// Override the "read" action with arbitrary logic.
get => $this->first . " " . $this->last;
// Override the "write" action with arbitrary logic.
set {
[$this->first, $this->last] = explode(' ', $value, 2);
$this->isModified = true;
}
}
}
The syntax doesn't require that both hooks always be defined
together; in fact, here's an example of only defining
set
from the RFC:
class User
{
public string $name {
set {
if (strlen($value) === 0) {
throw new ValueError("Name must be non-empty");
}
$this->name = $value;
}
}
public function __construct(string $name) {
$this->name = $name;
}
}
You can read all the details about Property Hooks in PHP in the RFC. This feature looks likely to drop in PHP 8.4. The implementation is already a draft pull request if you want to see the discussion and progress of this feature.
The post Property Hooks Get Closer to Becoming a Reality in PHP 8.4 appeared first on Laravel News.
Join the Laravel Newsletter to get all the latest Laravel articles like this directly in your inbox.
Read more https://laravel-news.com/php-property-hooks
Read more https://build.prestashop-project.org/news/2024/live-update-april-2024/
Designing a beautiful website from scratch can be difficult for developers of all skill levels. Luckily, in today’s Build and Beyond video, Jamie Marsland reveals his ten favorite WordPress design tools and websites to elevate your next build.
Get inspiration for your next website’s design and then start building with WordPress.com. Ready to get going? Click below to embark on your free trial today:
WordPress.com/JamieHere are the sites and resources mentioned in the video:
Stunning backgrounds and visuals
A design-your-own-theme tool using block patterns
Free CSS generator for a glass effect
Save and explore inspiring designs
Easy mockups for products and thumbnails
WordPress.com’s free library of block patterns
Generate color palettes with a click
Another block pattern library, but with community-uploaded designs
The best AI image generator
WordPress plugin to easily find free-to-use images
Read more https://wordpress.com/blog/2024/04/18/wordpress-design-resouces/
Designing a beautiful website from scratch can be difficult for developers of all skill levels. Luckily, in today’s Build and Beyond video, Jamie Marsland reveals his ten favorite WordPress design tools and websites to elevate your next build.
Get inspiration for your next website’s design and then start building with WordPress.com. Ready to get going? Click below to embark on your free trial today:
WordPress.com/JamieHere are the sites and resources mentioned in the video:
Stunning backgrounds and visuals
A design-your-own-theme tool using block patterns
Free CSS generator for a glass effect
Save and explore inspiring designs
Easy mockups for products and thumbnails
WordPress.com’s free library of block patterns
Generate color palettes with a click
Another block pattern library, but with community-uploaded designs
The best AI image generator
WordPress plugin to easily find free-to-use images
Read more https://wordpress.com/blog/2024/04/18/wordpress-design-resouces/
Laravel's recent release of Laravel
11.4 introduced the Exceptions
facade, which adds
conveniences around asserting exceptions in Laravel's exception
handler. Before this release, I would typically use the
$this->withoutExceptionHandling()
to assert that a
specific exception happened during an HTTP test:
use App\Exceptions\WelcomeException;
$this->withoutExceptionHandling();
try {
$this->get('/');
} catch (WelcomeException $e) {
$this->assertEquals('Woops, there was an issue with your request!', $e->getMessage());
return;
}
$this->fail(sprintf('The expected "%s" exception was not thrown.', WelcomeException::class));
When you expect a request to not throw any exceptions, using
withoutExceptionHandling
cuts out the middleman when
you're debugging why an error is happening when you don't expect
it. The above code is tedious to write, because it manually
captures the exception, makes assertions about the exception, and
calls return
to avoid the manual
$this->fail()
call. The manual failure will catch
situations when the test doesn't throw an exception when
expected.
If $this->fail()
is called in the above
scenario, here's what the output would look like:
$ phpunit
There was 1 failure:
1) Tests\Feature\ExampleTest::test_the_application_returns_a_successful_response
The expected "App\Exceptions\WelcomeException" exception was not thrown.
/app/tests/Feature/ExampleTest.php:33
Let's look at how the new Exceptions
facade can
simplify our test; the first example, rewritten, would look as
follows:
use Illuminate\Support\Facades\Exceptions;
Exceptions::fake();
$this->get('/');
Exceptions::assertReported(function (WelcomeException $e): bool {
return $e->getMessage() === "Woops, there was an issue with your request!";
});
Using the Exceptions facade gives us the bonus of not having to capture an exception to assert things manually. Said another way, the test can keep Laravel's exception handler in place but still be able to assert exceptions that happened during a request.
If we want to be sure that a test doesn't throw a specific exception or doesn't throw any exceptions, period, the new facade has our back:
Exceptions::assertNotReported(WelcomeException::class);
Exceptions::assertNothingReported();
If the test actually reported the WelcomeException
,
the test output would give us a nicely formatted message:
$ phpunit
There was 1 failure:
1) Tests\Feature\ExampleTest::test_the_application_returns_a_successful_response
The expected [App\Exceptions\WelcomeException] exception was not reported.
While there may be times when you don't want to fake Laravel's exception handler, when testing edge cases, the new Exceptions facade is tremendously helpful and cleans up our code:
Exceptions::assertReported(WelcomeException::class);
Exceptions::assertReportedCount($count);
Exceptions::assertNotReported(WelcomeException::class);
Exceptions::assertNothingReported();
Exceptions::throwFirstReported();
To learn more about the Exceptions facade, check out Laravel’s documentation.
The post Asserting Exceptions in Laravel Tests appeared first on Laravel News.
Join the Laravel Newsletter to get all the latest Laravel articles like this directly in your inbox.
Read more https://laravel-news.com/asserting-exceptions-laravel
Page 1 of 1309