From 7f71e3dc3bb4826a48172d2f008c3b7987948d8b Mon Sep 17 00:00:00 2001 From: Kevin Day Date: Sat, 5 Jan 2019 19:27:41 -0600 Subject: [PATCH] Feature: t_base_return class objects may now be used directly as strings By utilizing __toString(), all classes using t_base_return can now be directly used as a string. This saves the effort of writing $my_class->get_value() or $my_class->get_value_exact(). This directly returns the private value as a string, which is probably faster than returning an object and then calling a function to convert. Complex types, such as array, object, or resource, or converted to an empty string and must still be handled via function calls. --- common/base/classes/base_return.php | 36 ++++++++++++++++++++++++++++++++++++ common/base/traits/base_return.php | 12 ++++++++++++ 2 files changed, 48 insertions(+) diff --git a/common/base/classes/base_return.php b/common/base/classes/base_return.php index dc249b7..4ec8f76 100644 --- a/common/base/classes/base_return.php +++ b/common/base/classes/base_return.php @@ -841,6 +841,18 @@ class c_base_return_array extends c_base_return_value { } /** + * Provide to string object override. + * + * @return string + * An empty string. + * + * @see: http://php.net/manual/en/language.oop5.magic.php@object.tostring + */ + public function __toString() { + return ""; + } + + /** * Assign the value. * * @param array $value @@ -1213,6 +1225,18 @@ class c_base_return_object extends c_base_return_value { } /** + * Provide to string object override. + * + * @return string + * An empty string. + * + * @see: http://php.net/manual/en/language.oop5.magic.php@object.tostring + */ + public function __toString() { + return ""; + } + + /** * Assign the value. * * @param object $value @@ -1273,6 +1297,18 @@ class c_base_return_resource extends c_base_return_value { } /** + * Provide to string object override. + * + * @return string + * An empty string. + * + * @see: http://php.net/manual/en/language.oop5.magic.php@object.tostring + */ + public function __toString() { + return ""; + } + + /** * Assign the value. * * @param resource $value diff --git a/common/base/traits/base_return.php b/common/base/traits/base_return.php index 0ee0361..bf829dd 100644 --- a/common/base/traits/base_return.php +++ b/common/base/traits/base_return.php @@ -22,6 +22,18 @@ trait t_base_return_value { protected $value; /** + * Provide to string object override. + * + * @return string + * The string representation of the value contained in this object. + * + * @see: http://php.net/manual/en/language.oop5.magic.php@object.tostring + */ + public function __toString() { + return strval($this->value); + } + + /** * Assign the value. * * If the value is an object, then this should create a copy of the object (a clone). -- 1.8.3.1