PHP Classes

File: union_types.php

Recommend this page to a friend!
  Classes of Allan Kibet  >  Learn New PHP 8 Features  >  union_types.php  >  Download  
File: union_types.php
Role: Example script
Content type: text/plain
Description: Example and class source
Class: Learn New PHP 8 Features
Examples of how to use PHP 8 new features
Author: By
Last change:
Date: 8 months ago
Size: 524 bytes
 

Contents

Class file image Download
<?php

// Function can take multiple input types
function display_length(int|float $length) {
    echo
"The length is: {$length} <br />";
}

display_length(22.56);

display_length(10);

class
Customer
{
    public function
__toString(): string
   
{
        return
"I am the Customer class";
    }
}

class
Employee
{
    public function
__toString(): string
   
{
        return
"I am the Customer class";
    }
}

function
which_class(Customer|Employee $obj) {
    echo
$obj;
}

$obj = new Customer();

which_class($obj);

For more information send a message to info at phpclasses dot org.