DEV Community

Meow
Meow

Posted on

1

What i like on PHP 7.4

Ok there is more what i like but this is biggest chages for me.

Maybe someone can tell me im crazy but i realy like strongly typed languages. And with 7.4 relase php is closer to them. (and more will come in 8.0) .

Now my Model classes can look like this

class UserIndexViewModel
{
    protected int $id;
    protected string $email;
    protected int $groupId;
    protected string $groupName;
    protected bool $isActive;
    protected bool $isAdmin;
    protected FrozenTime $created;
    protected FrozenTime $modified;

    /**
     * UserIndexViewModel constructor.
     * @param int $id
     * @param string $email
     * @param int $groupId
     * @param string $groupName
     * @param bool $isActive
     * @param bool $isAdmin
     * @param FrozenTime $created
     * @param FrozenTime $modified
     */
    private function __construct(int $id, string $email, int $groupId, string $groupName, bool $isActive, bool $isAdmin, FrozenTime $created, FrozenTime $modified)
    {
        $this->id = $id;
        $this->email = $email;
        $this->groupId = $groupId;
        $this->groupName = $groupName;
        $this->isActive = $isActive;
        $this->isAdmin = $isAdmin;
        $this->created = $created;
        $this->modified = $modified;
    }

    /**
     * @return int
     */
    public function getId(): int
    {
        return $this->id;
    }
// ...
}
Enter fullscreen mode Exit fullscreen mode

One think i missing now is array of objects. In 7.4 you can not define it. You define simply just array however it is here a workaround and it can be defined in doc blocks:

   /**
     * Returns populated array f ViewModel
     *
     * @param \Cake\Datasource\ResultSetInterface|\Iam\Model\Entity\User[] $data
     * @param IdentityInterface $identity
     * @return array|\Iam\ViewModel\UserIndexViewModel[]
     */
    public static function prepare(ResultSetInterface $data, IdentityInterface $identity) : array
Enter fullscreen mode Exit fullscreen mode

PHP 8.0 (which is in RC stage i think) will bring Union types, so that can change.

What do you think about PHP now? Do you like weakly typed or strongly typed languages more?

Heroku

Simplify your DevOps and maximize your time.

Since 2007, Heroku has been the go-to platform for developers as it monitors uptime, performance, and infrastructure concerns, allowing you to focus on writing code.

Learn More

Top comments (2)

Collapse
 
catalinionut profile image
Catalin Ionut Titov

Why do you still use doc block if every property including the return type is type hinted? It's redudant.

Collapse
 
maymeow profile image
Meow • Edited

They are rewritten functions (i used 7.3 before)... but sometimes i have to use them for example if my return type is Array of something.... i cant return function xy() : User[] but only function xy(): array so i add @return array|User[] to docblock...

PS: and PhpStorm returns me errors when definition in docblock is missing... (i have to configure it to do not doing it)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more