|
Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon. |
|
View previous topic :: View next topic |
Author |
Message |
DJCMBear Smarty n00b
Joined: 18 Mar 2012 Posts: 1
|
Posted: Sun Mar 18, 2012 7:51 pm Post subject: Smarty Object as Array? |
|
|
I have been looking over the forum and have seen many threads about allowing the form of objects being called like arrays, from what I have found with developing PHP systems there are many ways to convert objects to arrays.
However with that said there are ways to not convert an object to an array but have an object act as an array to make calling object properties like you would any normal array, here is an example of this in action.
Code: | <?php
# Setting a demo class
class DemoClass {
/**
* Default class construct
*/
public function __construct () {}
/**
* Displays whatever was sent to it
* if the item is a valid string
*
* @param string $string
* @return void
*/
public function Display ($string) {
if (is_string($string)) {
print "{$string}<br />\n";
}
}
}
# Creating the ArrayObject to allow objects to be used as arrays
$Array = new ArrayObject(array(), ArrayObject::ARRAY_AS_PROPS);
# Setting the property 'Class' as a reference to the 'DemoClass'
$Array -> Class = new DemoClass;
# Checking if the property 'Class' is referenced in 'Array'
if (isset($Array["Class"]) && !empty($Array["Class"])) {
# Using the variable 'Array' as an array reference
$Array["Class"] -> Display("Hello World"); // Outputs 'Hello World'
# Using the variable 'Array' as an object reference
$Array -> Class -> Display("Hello World"); // Outputs 'Hello World'
}
?> |
So in affect a smarty template could contain this. The first string uses only one '->' due to it referencing a class method but if there were many levels to the object it would use more than just the one '.' in the string.
Code: | {$Array.Class -> Display("Hello World")}
// OR
{$Array -> Class -> Display("Hello World")} |
Maybe this could be used in a newer version of Smarty to allow this feature or even creating a new plugin for users to download to allow the option to add this feature in, Just a thought I had while looking over some of the threads.
--
- DJCMBear
-- |
|
Back to top |
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|