Home > Backend Development > PHP7 > body text

About scalar type and return value type declaration in PHP7

藏色散人
Release: 2023-02-17 18:02:01
forward
1794 people have browsed it

A question: Are you writing code using new features?

Strict mode
php declare(strict_types=1);

Settings passed in Parameters, and return values ​​of outgoing parameters<div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false">class Person(){     public function name(string $name):string    {         return &quot;名称:&quot;.$name;     }     public function age(int $age):string    {         return &quot;年龄:&quot;.$age;     }}</pre><div class="contentsignin">Copy after login</div></div>

Custom return type
class Address(){
    public function getAddress(int $age):string    {
        return ['street'=>'street1','country'=>'china'];
    }}class Person(){

    public function name(string $name):string    {
        return "名称:".$name;
    }

    public function age(int $age):string    {
        return "年龄:".$age;
    }

    public function age(int $age):Address    {
        return new Address();
    }}
Copy after login

The above is the detailed content of About scalar type and return value type declaration in PHP7. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:learnku.com
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template