Skip to content
Advertisement

Tag: types

Generic enum type in php

How can I specify an argument type to take any enum value? Something like function processEnum(enum $value) would be ideal, however nothing seems to exist? Additionally it would be nice to separate backed vs non-backed enums or additionally backed types; enums that are backed as strings for example. Answer All enums implement the UnitEnum interface, and backed enums (those with

How To Declare Traversable Type?

There are multiple typehints in PHP, more now in PHP 8, and even more with Psalm. But what about declaring the type, rather than hinting the expected type? I have a interface Collection extends Traversable, and I need to declare that it is a traversable of type Abc. How can I do that, so that PHPDoc or Psalm know that

How to add options to a Symfony Form field?

I am rather new to PHP & Symfony, and am struggling with the form options: I have the following, simple code: I get an error because the format is not an option of TextType, but I cannot find a way to add my own options (But I know this is possible, from the others posts I read) I have read

Laravel / PHPUnit: Asserting sameness of two models

When integration testing (using a database) in Laravel. What is the best way to assert that two instances of the same Eloquent model are the same? AssertEquals $model $model Simply using ->assertEquals($modelOne, $modelTwo); wont work as even if they are the same PHPUnit checks class properties such as wasRecentlyCreated and sometimes model ids can be a string or an integer.

With strict types enabled, array_map converts types anyways

I’ve came across a really interesting bug in PHP 7.0.11 where declare(strict_types=1); enabled does not make the array_map() nor array_walk() aware of strict types. I read somewhere that by enabling strict types PHP also uses strict types in core functions, but this is not the case. Look at this example code, it should throw TypeError exception: The var_dump result is:

MySQL: dates before 1970

I’m working on a church site which has many events before 1970, these events starts from 1001 to today, how can I store the dates in MySQL? This works fine only for dates after 1970, how can I store dates before this year? What kind of datatype should I have in MySQL? Now I’ve set my column to date type

Advertisement