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 @@ -840,6 +840,18 @@ class c_base_return_array extends c_base_return_value { return self::p_s_value_exact($return, __CLASS__, []); } + /** + * 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. * @@ -1212,6 +1224,18 @@ class c_base_return_object extends c_base_return_value { return self::p_s_value($return, __CLASS__); } + /** + * 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. * @@ -1272,6 +1296,18 @@ class c_base_return_resource extends c_base_return_value { return self::p_s_value($return, __CLASS__); } + /** + * 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. * 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 @@ -21,6 +21,18 @@ namespace n_koopa; 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. * -- 2.47.3