Can the type of the return value of a PHP function be an array, object, or instance of a class?

王林
Release: 2024-04-15 11:03:02
Original
430 people have browsed it

PHP functions can return arrays, objects or class instances: 1. Array: use square brackets; 2. Object: use the new keyword to create an object; 3. Class instance: omit the new keyword. Practical case: getUsers() returns an array of users, and createUser() creates a user object.

PHP 函数返回值的类型是否可以为数组、对象或类的实例?

PHP function return value type: array, object, class instance

PHP function can return various types of values, including Instances of arrays, objects, and classes.

Array

To return an array as a function return value, use square brackets:

<?php
function getArray(): array
{
    return [1, 2, 3];
}
?>
Copy after login

Object

To return an object, create the object using the new keyword as follows:

<?php
class Person
{
    private $name;

    public function __construct(string $name)
    {
        $this->name = $name;
    }

    public function getName(): string
    {
        return $this->name;
    }
}

function getObject(): Person
{
    return new Person('John Doe');
}
?>
Copy after login

Instance of class

Returning an instance of a class is similar to returning an object , but you can omit the new keyword, as shown below:

<?php
class Animal
{
    private $species;

    public function __construct(string $species)
    {
        $this->species = $species;
    }

    public function getSpecies(): string
    {
        return $this->species;
    }
}

function getInstance(): Animal
{
    return Animal('Dog');
}
?>
Copy after login

Practical case

Suppose you have a function to obtain the detailed information of a batch of users:

function getUsers(): array
{
    // ... 数据库查询,返回用户数组
}
Copy after login

To use this function in a controller you would:

<?php

$users = getUsers();

// 遍历用户数组
foreach ($users as $user) {
    // ...
}
?>
Copy after login

Similarly, if you have a function that creates a new user object:

function createUser(string $name, string $email): Person
{
    // ... 数据库查询,返回新的用户对象
}
Copy after login

To use it in Using this function in your model you can:

<?php

$user = createUser('John Doe', 'john.doe@example.com');

// 访问用户属性
echo $user->getName(); // 输出:John Doe
?>
Copy after login

The above is the detailed content of Can the type of the return value of a PHP function be an array, object, or instance of a class?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!