ToStringTrait uses trait:short
Trait ToStringTrait
Provides a basic string representation for an object by returning its class name wrapped in square brackets or other defined characters.
This trait uses reflection to dynamically obtain the short class name of the object.
It also caches the result for performance on repeated calls to __toString()
.
Methods:
- __toString(): Returns a string like "[ClassName]".
Example usage:
use oihana\traits\ToStringTrait;
class MyObject {
use ToStringTrait;
}
echo (new MyObject()); // Output: [MyObject]
Table of Contents
Properties
- $__className : string|null
- $__reflection : Reflection|null
Methods
- __toString() : string
- Returns a String representation of the object.
- getClassName() : string
- Returns the short class name of the given object/class.
- getConstants() : array<string|int, mixed>
- Returns the list of all constants of the given object/class.
- getPublicProperties() : array<string|int, mixed>
- Internal methods to get the public
- getShortName() : string
- Returns the class short name.
- hydrate() : object
- Populates an object of the given class with data from the provided array.
- jsonSerializeFromPublicProperties() : array<string|int, mixed>
- Invoked to generates the json array serializer array representation from the public properties of the object.
Properties
$__className
private
string|null
$__className
= null
$__reflection
private
Reflection|null
$__reflection
= null
Methods
__toString()
Returns a String representation of the object.
public
__toString() : string
Tags
Return values
string —A string representation of the object.
getClassName()
Returns the short class name of the given object/class.
public
getClassName(object|string $class) : string
Parameters
- $class : object|string
-
The object or the classname reference.
Return values
stringgetConstants()
Returns the list of all constants of the given object/class.
public
getConstants(object|string $class) : array<string|int, mixed>
Parameters
- $class : object|string
-
The object or the classname reference.
Tags
Return values
array<string|int, mixed>getPublicProperties()
Internal methods to get the public
public
getPublicProperties(object|string $class) : array<string|int, mixed>
Parameters
- $class : object|string
-
The object or the classname reference.
Tags
Return values
array<string|int, mixed>getShortName()
Returns the class short name.
public
getShortName(object|string $class) : string
Parameters
- $class : object|string
-
The object or the classname reference.
Tags
Return values
stringhydrate()
Populates an object of the given class with data from the provided array.
public
hydrate(array<string|int, mixed> $thing, string $class) : object
Parameters
- $thing : array<string|int, mixed>
-
The data array used to hydrate the object.
- $class : string
-
The classname of the object to be hydrated.
Tags
Return values
object —The hydrated object of the given class.
jsonSerializeFromPublicProperties()
Invoked to generates the json array serializer array representation from the public properties of the object.
public
jsonSerializeFromPublicProperties(object|string $class[, bool $reduce = false ]) : array<string|int, mixed>
Parameters
- $class : object|string
-
The classname of the object to be serialized.
- $reduce : bool = false
-
Indicates if the returned associative array is compressed, the null properties are removed.