PHP-эквивалент .NET/Java toString()

Это делается с помощью приведения php5 типов:

$strvar = (string) $var; // Casts to string
echo $var; // Will cast to string implicitly
var_dump($var); // Will show the true type of the variable

В классе вы можете php-oop определить, что выводить, используя php-mail волшебный метод __toString. Пример php-frameworks ниже:

class Bottles {
    public function __toString()
    {
        return 'Ninety nine green bottles';
    }
}

$ex = new Bottles;
var_dump($ex, (string) $ex);
// Returns: instance of Bottles and "Ninety nine green bottles"

Еще несколько примеров php-include приведения типов:

$i = 1;

// int 1
var_dump((int) $i);

// bool true
var_dump((bool) $i);

// string "1"
var_dump((string) 1);

php

string

2022-11-20T06:21:52+00:00