search
HomeBackend DevelopmentPHP7The meaning of php7 type constraints

The meaning of php7 type constraints

The meaning of php7 type constraints

beforePHP7 , functions and class methods do not need to declare variable types, and any data can be passed and returned, causing almost most calling operations to determine whether the returned data type is qualified.

To solve this problem, PHP7 introduced type declarations.

There are currently two types of variables that can be declared: formal parameters and return values.

Supported types include integer, floating point, string, and Boolean.

The following code:

<?php
function study(int $get)
{
    return $get;
}
var_dump(study(&#39;ddd&#39;));

In order to save the length of the code, I will not write OOP, just use the function as an example.

This function specifies that the parameter type is int, and the 'ddd' string is passed in when it is called, so a Fatal error will occur when running this code.

In some cases, PHP is always not so serious, as follows:

<?php
function study(bool $get)
{
    return $get;
}
var_dump(study(&#39;ddd&#39;));

The specified parameter type is bool, but the incoming string is converted into true, The output result after running is: bool(true)

If you want to force the constraint type, you can add a declare statement at the head of the file:

<?php
declare(strict_types = 1);
function study(bool $get)
{
    return $get;
}
var_dump(study(&#39;ddd&#39;));

At this time, it will also A fatal error occurs because the specified parameter type is bool but the passed parameter type is string. Only changing 'ddd' to true and false will work, even if it is 1 or 0.

Next, let’s introduce the return type declaration:

<?php
declare(strict_types = 1);
function study(bool $get) : int
{
    return (int)$get;
}
var_dump(study(true));

Add a colon and type after the parentheses of the formal parameter to specify the type of method return value and what type of return is specified. That's what type.

It is also affected by declare.

If there is no type mandatory constraint, then PHP will convert the returned data into the specified type of data and then return it. Currently, only string cannot be forced to int. An error will be reported.

If mandatory constraints are specified, PHP will not perform type conversion, but will compare. If the types are different, an error will be reported.

Also, the return value type can also specify the object name, then the instance of the specified object must be returned, such as:

<?php
class person
{
    private function __construct($name)
    {
        $this->name = $name;
    }
    static function instance(string $name) : person
    {
        return new self($name);
    }
}
person::instance(&#39;zhangsan&#39;);

This is One of the new features of PHP7: type declaration.

The above is the detailed content of The meaning of php7 type constraints. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function