ArrayAccess in extension

Author Message

A Costello

Sunday 06 July 2008 3:14:20 pm

I have an extension that makes use of the ArrayAccess interface. The template system does not correctly handle the array access. It always returns null. Is there anyway to work around this?

André R.

Monday 07 July 2008 2:19:57 am

ArrayAccess??

eZ Online Editor 5: http://projects.ez.no/ezoe || eZJSCore (Ajax): http://projects.ez.no/ezjscore || eZ Publish EE http://ez.no/eZPublish/eZ-Publish-Enterprise-Subscription
@: http://twitter.com/andrerom

A Costello

Monday 07 July 2008 9:25:28 pm

ArrayAccess is an interface for php classes. It is quite handy. I am sending you a demo.

class Demo implements ArrayAccess, Iterator 
{
	 private $demo = 'default';
	 private $test = 'default';
	 private $_offsets = array('Demo', 'Test');       
	 
	 public function setDemo($value)
	 {
	 	 $this->demo = $value;	 	 
	 }
	 
	 public function getDemo()
	 {
	 	 return $this->demo;
	 }
	 
	 public function setTest($value)
	 {
	 	 $this->test = $value;
	 }
	 
	 public function getTest()
	 {
	 	 return $this->test;
	 }
	 
	//Foreach Access
    public function rewind()
    {
        $this->cur = 0;
    }
    
    public function key()
    {
        return $this->_offsets[$this->cur];
    }
    
    public function current()
    {   
        $function = 'get' . $this->_offsets[$this->cur];
        return $this->$function();   
    }
    
    public function next()
    {
        ++$this->cur;
    }
    
    public function valid()
    {
        return $this->cur < count($this->_offsets);
    }
    
    //Array Accessor
    function offsetExists($name) 
    {
        return (in_array($name, $this->_offsets));
    }
    
    function offsetGet($name) 
    {
        if(in_array($name, $this->_offsets))
        {
            $function = 'get' . $name;  
            return $this->$function();
        }
        return null;                         
    }
    
    function offsetSet($name, $id) 
    {
        if(in_array($name, $this->_offsets))
        {
            $function = 'set' . $name;
            return $this->$function($id);
        }                                     
    }
    
    function offsetUnset($name) 
    {
        if(in_array($name, $this->_offsets))
        {
            $function = 'set' . $name;
            return $this->$function(null);
        }
    }
}     

$example = new Demo();
echo '<h4>Before Set</h4>';
echo 'demo = ' . $example['Demo'] . '<br>';
echo 'test = ' . $example['Test'] . '<br>';   


$example['Demo'] = 'Demo';
$example['Test'] = 'Test';
echo '<h4>After Set</h4>';
echo 'demo = ' . $example['Demo'] . '<br>';
echo 'test = ' . $example['Test'] . '<br>';   

echo '<h4>Foreach</h4>';
foreach($example as $key => $value)
{
	echo "$key = $value <br>";
}

echo '<h4>Work Around</h4>';
foreach($example as $key => $value)
{
	$$key = $value;
	//Set to template variable
}
echo  'demo = ' . $Demo . '<br>';
echo 'test = ' . $Test . '<br>';  

André R.

Tuesday 08 July 2008 3:49:37 am

Ok, good to know what we're talking about :)
You need to implement hasAttribute, attribute and attributes functions when you want to use a object in the template language. Look at for instance kernel/classes/ezpersistentobject.php

eZ Online Editor 5: http://projects.ez.no/ezoe || eZJSCore (Ajax): http://projects.ez.no/ezjscore || eZ Publish EE http://ez.no/eZPublish/eZ-Publish-Enterprise-Subscription
@: http://twitter.com/andrerom

Powered by eZ Publish™ CMS Open Source Web Content Management. Copyright © 1999-2014 eZ Systems AS (except where otherwise noted). All rights reserved.

eZ debug

Timing: Jan 30 2025 21:12:23
Script start
Timing: Jan 30 2025 21:12:23
Module start 'layout'
Timing: Jan 30 2025 21:12:23
Module start 'content'
Timing: Jan 30 2025 21:12:23
Module end 'content'
Timing: Jan 30 2025 21:12:23
Script end

Main resources:

Total runtime0.0205 sec
Peak memory usage4,096.0000 KB
Database Queries3

Timing points:

CheckpointStart (sec)Duration (sec)Memory at start (KB)Memory used (KB)
Script start 0.00000.0046 588.1328151.2109
Module start 'layout' 0.00460.0030 739.3438220.6875
Module start 'content' 0.00760.0114 960.03131,001.9141
Module end 'content' 0.01900.0015 1,961.945337.9922
Script end 0.0204  1,999.9375 

Time accumulators:

 Accumulator Duration (sec) Duration (%) Count Average (sec)
Ini load
Load cache0.002512.3260140.0002
Check MTime0.00115.1901140.0001
Mysql Total
Database connection0.00073.662910.0007
Mysqli_queries0.00199.320630.0006
Looping result0.00000.043110.0000
Template Total0.00105.010.0010
Template load0.00084.024310.0008
Template processing0.00020.880210.0002
Override
Cache load0.00062.816610.0006
General
dbfile0.00125.669380.0001
String conversion0.00000.033840.0000
Note: percentages do not add up to 100% because some accumulators overlap

Templates used to render the page:

UsageRequested templateTemplateTemplate loadedEditOverride
1print_pagelayout.tpl<No override>extension/community/design/community/templates/print_pagelayout.tplEdit templateOverride template
 Number of times templates used: 1
 Number of unique templates used: 1

Time used to render debug report: 0.0001 secs