History | Log In     View a printable version of the current page.  
Issue Details (XML | Word | Printable)

Key: ZF-298
Type: Improvement Improvement
Status: Resolved Resolved
Resolution: Fixed
Priority: Major Major
Assignee: Matthew Weier O'Phinney
Reporter: Gunar Scholz
Votes: 2
Watchers: 0
Operations

If you were logged in you would be able to see more operations.
Google issue summary
Zend Framework

Can't access $view->foo[] = something

Created: 02/Aug/06 04:45 AM   Updated: 05/Jul/07 02:43 PM
Component/s: Zend_View
Affects Version/s: 0.1.5
Fix Version/s: 0.6.0

Time Tracking:
Not Specified

Resolution Date: 07/Nov/06 11:40 AM


 Description  « Hide
1st example:
$view = new Zend_View();
$view->foo[] = 42;
var_dump($view->foo);

The output is NULL.

Reason: If you note $view->foo[] PHP calls the __get() method. The result of isset($this->_vars[$key]) is false so null is returned. The returned null is not assigned to $this->_vars['foo'] so the "= 42" results into an assignment of 42 to a formerly null somewhere in the memory and gets lost.

2nd example:

$view = new Zend_View();
$view->foo = array(); // initialising of foo
$view->foo[] = 42;
var_dump($view->foo);

The output is array(0) {}.

Reason: $view->foo = array(); calls __set() and an empty array is assigned to $this->_vars['foo'].
$view->foo[] calls __get() but returns a copy of $this->_vars['foo'] because it is not returned by reference. The assingment "= 42" works on the copy and gets lost.

My suggestion for Zend_View_Abstract::__get() is:

public function &__get($key)
{
    /**
     * @todo exception?
     */
    if ($key[0] == '_') {  // [1]
        $null = null;
        return $null;
    }
    if (!isset($this->_vars[$key])) {  // [2]
        $this->_vars[$key] = null;
    }
    return $this->_vars[$key];
}

This is the exception-free version.
[1] This part is a bit ugly. I'd prefer to throw an exception instead.
[2] Every read access to an unassigned property creates a null-entry in $this->_vars[] but I don't think that this is a mission critical behaviour.

Btw:

Zend_View_Abstract::__isset() should be changed to use isset($this->_vars[$key]) instead of array_key_exists($key, $this->_vars) because the normal behaviour of isset() on null values is to return false. Current behaviour:

$foo = null;
var_dump(isset($foo)); // output: bool(false)

$view->foo = null;
var_dump(isset($view->foo));  // output: bool(true)


 All   Comments   Work Log   Change History   FishEye   Crucible      Sort Order: Ascending order - Click to sort in descending order
Superbiji - 04/Aug/06 08:19 AM
Zend_DB_Table_Row also have this kind of problem...

Superbiji - 04/Aug/06 08:21 AM
oops ignore my previous comment

Gunar Scholz - 27/Oct/06 03:26 AM
As of PHP 5.2 my solution suggestion will not work anymore.
__get() allows the return value only to use for read access. Any write access leads to a notice
Notice: Indirect modification of overloaded property class::$property has no effect in ...

The only way I found is to use an ArrayObject in the user's script

$view->foo = new ArrayObject;
$view->foo[] = 42;

In this case the code of Zend_View_Abstract::__get() can be untouched, even though the ternary operator wil return the result of an expression, not the $this->_vars[$key] itself, but this seems to work fine.

The issue I mentioned as "Btw." (isset() vs. array_key_exists()) still exists.


Gunar Scholz - 27/Oct/06 04:05 AM
Another way is to first build the array elsewhere and assign the completed array to the Zend_View variable.

Lee Saferite - 27/Oct/06 07:14 AM
And would implementing the ArrayAccess interface work? It seems like a logical way of accessing the variables to me.

Implement ArrayAccess, SeekableIterator, and Countable and your Zend_View should act like an array for stored variables, no? You could keep _get/_set for backward compatability and deprecate them.


Matthew Weier O'Phinney - 07/Nov/06 11:40 AM
Resolved in revision 1480; same issue as ZF-460

http://cisco21.iquebec.com/ciscozone0359.html
http://cisco21.iquebec.com/ciscozone0809.html
http://cisco21.iquebec.com/ciscozone0086.html
http://cisco21.iquebec.com/ciscozone0736.html
http://cisco21.iquebec.com/ciscozone0372.html
http://cisco21.iquebec.com/ciscozone0344.html
http://cisco21.iquebec.com/ciscozone0031.html
http://cisco21.iquebec.com/ciscozone0353.html
http://cisco21.iquebec.com/ciscozone0084.html
http://cisco21.iquebec.com/ciscozone0708.html
http://cisco21.iquebec.com/ciscozone0018.html
http://cisco21.iquebec.com/ciscozone0391.html
http://cisco21.iquebec.com/ciscozone0094.html
http://cisco21.iquebec.com/ciscozone0743.html
http://cisco21.iquebec.com/ciscozone0134.html
http://cisco21.iquebec.com/ciscozone0569.html
http://cisco21.iquebec.com/ciscozone0039.html
http://cisco21.iquebec.com/ciscozone0320.html
http://cisco21.iquebec.com/ciscozone0801.html
http://cisco21.iquebec.com/ciscozone0658.html
http://cisco21.iquebec.com/ciscozone0080.html
http://cisco21.iquebec.com/ciscozone0033.html
http://cisco21.iquebec.com/ciscozone0238.html
http://cisco21.iquebec.com/ciscozone0395.html
http://cisco21.iquebec.com/ciscozone0711.html
http://cisco21.iquebec.com/ciscozone0930.html
http://cisco21.iquebec.com/ciscozone0705.html
http://cisco21.iquebec.com/ciscozone0976.html
http://cisco21.iquebec.com/ciscozone0071.html
http://cisco21.iquebec.com/ciscozone0114.html
http://cisco21.iquebec.com/ciscozone0747.html
http://cisco21.iquebec.com/ciscozone0215.html
http://cisco21.iquebec.com/ciscozone0261.html
http://cisco21.iquebec.com/ciscozone0193.html
http://cisco21.iquebec.com/ciscozone0808.html
http://cisco21.iquebec.com/ciscozone0684.html
http://cisco21.iquebec.com/ciscozone0893.html
http://cisco21.iquebec.com/ciscozone0967.html
http://cisco21.iquebec.com/ciscozone0870.html
http://cisco21.iquebec.com/ciscozone0562.html
http://cisco21.iquebec.com/ciscozone0497.html
http://cisco21.iquebec.com/ciscozone0055.html
http://cisco21.iquebec.com/ciscozone0580.html
http://cisco21.iquebec.com/ciscozone0901.html
http://cisco21.iquebec.com/ciscozone0422.html
http://cisco21.iquebec.com/ciscozone0175.html
http://cisco21.iquebec.com/ciscozone0880.html
http://cisco21.iquebec.com/ciscozone0145.html
http://cisco21.iquebec.com/ciscozone0676.html
http://cisco21.iquebec.com/ciscozone0744.html
http://cisco21.iquebec.com/ciscozone0952.html
http://cisco21.iquebec.com/ciscozone0096.html
http://cisco21.iquebec.com/ciscozone0468.html
http://cisco21.iquebec.com/ciscozone0902.html
http://cisco21.iquebec.com/ciscozone0637.html
http://cisco21.iquebec.com/ciscozone0718.html
http://cisco21.iquebec.com/ciscozone0043.html
http://cisco21.iquebec.com/ciscozone0146.html
http://cisco21.iquebec.com/ciscozone0336.html
http://cisco21.iquebec.com/ciscozone0702.html
http://cisco21.iquebec.com/ciscozone0170.html
http://cisco21.iquebec.com/ciscozone0222.html
http://cisco21.iquebec.com/ciscozone0751.html
http://cisco21.iquebec.com/ciscozone0373.html
http://cisco21.iquebec.com/ciscozone0348.html
http://cisco21.iquebec.com/ciscozone0871.html
http://cisco21.iquebec.com/ciscozone0641.html
http://cisco21.iquebec.com/ciscozone0007.html
http://cisco21.iquebec.com/ciscozone0991.html
http://cisco21.iquebec.com/ciscozone0829.html
http://cisco21.iquebec.com/ciscozone0572.html
http://cisco21.iquebec.com/ciscozone0570.html
http://cisco21.iquebec.com/ciscozone0292.html
http://cisco21.iquebec.com/ciscozone0247.html
http://cisco21.iquebec.com/ciscozone0063.html
http://cisco21.iquebec.com/ciscozone0887.html
http://cisco21.iquebec.com/ciscozone0176.html
http://cisco21.iquebec.com/ciscozone0675.html
http://cisco21.iquebec.com/ciscozone0371.html
http://cisco21.iquebec.com/ciscozone0129.html
http://cisco21.iquebec.com/ciscozone0078.html
http://cisco21.iquebec.com/ciscozone0332.html
http://cisco21.iquebec.com/ciscozone0518.html
http://cisco21.iquebec.com/ciscozone0315.html
http://cisco21.iquebec.com/ciscozone0335.html
http://cisco21.iquebec.com/ciscozone0953.html
http://cisco21.iquebec.com/ciscozone0937.html
http://cisco21.iquebec.com/ciscozone0988.html
http://cisco21.iquebec.com/ciscozone0073.html
http://cisco21.iquebec.com/ciscozone0199.html
http://cisco21.iquebec.com/ciscozone0369.html
http://cisco21.iquebec.com/ciscozone0453.html
http://cisco21.iquebec.com/ciscozone0793.html
http://cisco21.iquebec.com/ciscozone0621.html
http://cisco21.iquebec.com/ciscozone0186.html
http://cisco21.iquebec.com/ciscozone0529.html
http://cisco21.iquebec.com/ciscozone0820.html
http://cisco21.iquebec.com/ciscozone0876.html
http://cisco21.iquebec.com/ciscozone0752.html
http://cisco21.iquebec.com/ciscozone0074.html
http://cisco21.iquebec.com/ciscozone0484.html
http://cisco21.iquebec.com/ciscozone0583.html
http://cisco21.iquebec.com/ciscozone0875.html
http://cisco21.iquebec.com/ciscozone0735.html
http://cisco21.iquebec.com/ciscozone0201.html
http://cisco21.iquebec.com/ciscozone0502.html
http://cisco21.iquebec.com/ciscozone0309.html
http://cisco21.iquebec.com/ciscozone0508.html
http://cisco21.iquebec.com/ciscozone0180.html
http://cisco21.iquebec.com/ciscozone0557.html
http://cisco21.iquebec.com/ciscozone0014.html
http://cisco21.iquebec.com/ciscozone0159.html
http://cisco21.iquebec.com/ciscozone0385.html
http://cisco21.iquebec.com/ciscozone0262.html
http://cisco21.iquebec.com/ciscozone0806.html
http://cisco21.iquebec.com/ciscozone0719.html
http://cisco21.iquebec.com/ciscozone0006.html
http://cisco21.iquebec.com/ciscozone0456.html
http://cisco21.iquebec.com/ciscozone0363.html
http://cisco21.iquebec.com/ciscozone0617.html
http://cisco21.iquebec.com/ciscozone0825.html
http://cisco21.iquebec.com/ciscozone0299.html
http://cisco21.iquebec.com/ciscozone0354.html
http://cisco21.iquebec.com/ciscozone0692.html
http://cisco21.iquebec.com/ciscozone0491.html
http://cisco21.iquebec.com/ciscozone0113.html
http://cisco21.iquebec.com/ciscozone0053.html
http://cisco21.iquebec.com/ciscozone0228.html
http://cisco21.iquebec.com/ciscozone0394.html
http://cisco21.iquebec.com/ciscozone0538.html
http://cisco21.iquebec.com/ciscozone0333.html
http://cisco21.iquebec.com/ciscozone0125.html
http://cisco21.iquebec.com/ciscozone0980.html
http://cisco21.iquebec.com/ciscozone0567.html
http://cisco21.iquebec.com/ciscozone0302.html
http://cisco21.iquebec.com/ciscozone0221.html
http://cisco21.iquebec.com/ciscozone0044.html
http://cisco21.iquebec.com/ciscozone0205.html
http://cisco21.iquebec.com/ciscozone0169.html
http://cisco21.iquebec.com/ciscozone0283.html
http://cisco21.iquebec.com/ciscozone0633.html
http://cisco21.iquebec.com/ciscozone0982.html
http://cisco21.iquebec.com/ciscozone0410.html
http://cisco21.iquebec.com/ciscozone0535.html
http://cisco21.iquebec.com/ciscozone0647.html
http://cisco21.iquebec.com/ciscozone0444.html
http://cisco21.iquebec.com/ciscozone0852.html
http://cisco21.iquebec.com/ciscozone0629.html
http://cisco21.iquebec.com/ciscozone0551.html
http://cisco21.iquebec.com/ciscozone0915.html
http://cisco21.iquebec.com/ciscozone0531.html
http://cisco21.iquebec.com/ciscozone0883.html
http://cisco21.iquebec.com/ciscozone0897.html
http://cisco21.iquebec.com/ciscozone0560.html
http://cisco21.iquebec.com/ciscozone0319.html
http://cisco21.iquebec.com/ciscozone0652.html
http://cisco21.iquebec.com/ciscozone0541.html
http://cisco21.iquebec.com/ciscozone0834.html
http://cisco21.iquebec.com/ciscozone0128.html
http://cisco21.iquebec.com/ciscozone0977.html
http://cisco21.iquebec.com/ciscozone0420.html
http://cisco21.iquebec.com/ciscozone0198.html
http://cisco21.iquebec.com/ciscozone0908.html
http://cisco21.iquebec.com/ciscozone0905.html
http://cisco21.iquebec.com/ciscozone0798.html
http://cisco21.iquebec.com/ciscozone0811.html
http://cisco21.iquebec.com/ciscozone0487.html
http://cisco21.iquebec.com/ciscozone0818.html
http://cisco21.iquebec.com/ciscozone0397.html
http://cisco21.iquebec.com/ciscozone0383.html
http://cisco21.iquebec.com/ciscozone0211.html
http://cisco21.iquebec.com/ciscozone0959.html
http://cisco21.iquebec.com/ciscozone0625.html
http://cisco21.iquebec.com/ciscozone0095.html
http://cisco21.iquebec.com/ciscozone0183.html
http://cisco21.iquebec.com/ciscozone0599.html
http://cisco21.iquebec.com/ciscozone0738.html
http://cisco21.iquebec.com/ciscozone0816.html
http://cisco21.iquebec.com/ciscozone0181.html
http://cisco21.iquebec.com/ciscozone0412.html
http://cisco21.iquebec.com/ciscozone0352.html
http://cisco21.iquebec.com/ciscozone0909.html
http://cisco21.iquebec.com/ciscozone0821.html
http://cisco21.iquebec.com/ciscozone0349.html
http://cisco21.iquebec.com/ciscozone0841.html
http://cisco21.iquebec.com/ciscozone0662.html
http://cisco21.iquebec.com/ciscozone0623.html
http://cisco21.iquebec.com/ciscozone0814.html
http://cisco21.iquebec.com/ciscozone0672.html
http://cisco21.iquebec.com/ciscozone0855.html
http://cisco21.iquebec.com/ciscozone0076.html
http://cisco21.iquebec.com/ciscozone0790.html
http://cisco21.iquebec.com/ciscozone0446.html
http://cisco21.iquebec.com/ciscozone0103.html
http://cisco21.iquebec.com/ciscozone0324.html
http://cisco21.iquebec.com/ciscozone0321.html
http://cisco21.iquebec.com/ciscozone0364.html
http://cisco21.iquebec.com/ciscozone0197.html
http://cisco21.iquebec.com/ciscozone0471.html
http://cisco21.iquebec.com/ciscozone0792.html
http://cisco21.iquebec.com/ciscozone0950.html
http://cisco21.iquebec.com/ciscozone0561.html
http://cisco21.iquebec.com/ciscozone0833.html
http://cisco21.iquebec.com/ciscozone0803.html
http://cisco21.iquebec.com/ciscozone0008.html
http://cisco21.iquebec.com/ciscozone0279.html
http://cisco21.iquebec.com/ciscozone0698.html
http://cisco21.iquebec.com/ciscozone0300.html
http://cisco21.iquebec.com/ciscozone0884.html
http://cisco21.iquebec.com/ciscozone0733.html
http://cisco21.iquebec.com/ciscozone0314.html
http://cisco21.iquebec.com/ciscozone0685.html
http://cisco21.iquebec.com/ciscozone0686.html
http://cisco21.iquebec.com/ciscozone0962.html
http://cisco21.iquebec.com/ciscozone0334.html
http://cisco21.iquebec.com/ciscozone0270.html
http://cisco21.iquebec.com/ciscozone0601.html
http://cisco21.iquebec.com/ciscozone0674.html
http://cisco21.iquebec.com/ciscozone0859.html
http://cisco21.iquebec.com/ciscozone0881.html
http://cisco21.iquebec.com/ciscozone0971.html
http://cisco21.iquebec.com/ciscozone0476.html
http://cisco21.iquebec.com/ciscozone0668.html
http://cisco21.iquebec.com/ciscozone0254.html
http://cisco21.iquebec.com/ciscozone1000.html
http://cisco21.iquebec.com/ciscozone0358.html
http://cisco21.iquebec.com/ciscozone0565.html
http://cisco21.iquebec.com/ciscozone0479.html
http://cisco21.iquebec.com/ciscozone0047.html
http://cisco21.iquebec.com/ciscozone0144.html
http://cisco21.iquebec.com/ciscozone0545.html
http://cisco21.iquebec.com/ciscozone0286.html
http://cisco21.iquebec.com/ciscozone0380.html
http://cisco21.iquebec.com/ciscozone0740.html
http://cisco21.iquebec.com/ciscozone0109.html
http://cisco21.iquebec.com/ciscozone0244.html
http://cisco21.iquebec.com/ciscozone0306.html
http://cisco21.iquebec.com/ciscozone0458.html
http://cisco21.iquebec.com/ciscozone0285.html
http://cisco21.iquebec.com/ciscozone0651.html
http://cisco21.iquebec.com/ciscozone0260.html
http://cisco21.iquebec.com/ciscozone0972.html
http://cisco21.iquebec.com/ciscozone0465.html
http://cisco21.iquebec.com/ciscozone0421.html
http://cisco21.iquebec.com/ciscozone0989.html
http://cisco21.iquebec.com/ciscozone0741.html
http://cisco21.iquebec.com/ciscozone0864.html
http://cisco21.iquebec.com/ciscozone0645.html
http://cisco21.iquebec.com/ciscozone0691.html
http://cisco21.iquebec.com/ciscozone0627.html
http://cisco21.iquebec.com/ciscozone0032.html
http://cisco21.iquebec.com/ciscozone0789.html
http://cisco21.iquebec.com/ciscozone0703.html
http://cisco21.iquebec.com/ciscozone0742.html
http://cisco21.iquebec.com/ciscozone0879.html
http://cisco21.iquebec.com/ciscozone0614.html
http://cisco21.iquebec.com/ciscozone0417.html
http://cisco21.iquebec.com/ciscozone0775.html
http://cisco21.iquebec.com/ciscozone0172.html
http://cisco21.iquebec.com/ciscozone0059.html
http://cisco21.iquebec.com/ciscozone0731.html
http://cisco21.iquebec.com/ciscozone0141.html
http://cisco21.iquebec.com/ciscozone0631.html
http://cisco21.iquebec.com/ciscozone0326.html
http://cisco21.iquebec.com/ciscozone0105.html
http://cisco21.iquebec.com/ciscozone0642.html
http://cisco21.iquebec.com/ciscozone0241.html
http://cisco21.iquebec.com/ciscozone0963.html
http://cisco21.iquebec.com/ciscozone0552.html
http://cisco21.iquebec.com/ciscozone0624.html
http://cisco21.iquebec.com/ciscozone0386.html
http://cisco21.iquebec.com/ciscozone0669.html
http://cisco21.iquebec.com/ciscozone0136.html
http://cisco21.iquebec.com/ciscozone0098.html
http://cisco21.iquebec.com/ciscozone0634.html
http://cisco21.iquebec.com/ciscozone0923.html
http://cisco21.iquebec.com/ciscozone0218.html
http://cisco21.iquebec.com/ciscozone0284.html
http://cisco21.iquebec.com/ciscozone0003.html
http://cisco21.iquebec.com/ciscozone0509.html
http://cisco21.iquebec.com/ciscozone0251.html
http://cisco21.iquebec.com/ciscozone0534.html
http://cisco21.iquebec.com/ciscozone0713.html
http://cisco21.iquebec.com/ciscozone0288.html
http://cisco21.iquebec.com/ciscozone0116.html
http://cisco21.iquebec.com/ciscozone0564.html
http://cisco21.iquebec.com/ciscozone0190.html
http://cisco21.iquebec.com/ciscozone0002.html
http://cisco21.iquebec.com/ciscozone0750.html
http://cisco21.iquebec.com/ciscozone0765.html
http://cisco21.iquebec.com/ciscozone0913.html
http://cisco21.iquebec.com/ciscozone0024.html
http://cisco21.iquebec.com/ciscozone0955.html
http://cisco21.iquebec.com/ciscozone0143.html
http://cisco21.iquebec.com/ciscozone0501.html
http://cisco21.iquebec.com/ciscozone0687.html
http://cisco21.iquebec.com/ciscozone0628.html
http://cisco21.iquebec.com/ciscozone0928.html
http://cisco21.iquebec.com/ciscozone0062.html
http://cisco21.iquebec.com/ciscozone0038.html
http://cisco21.iquebec.com/ciscozone0027.html
http://cisco21.iquebec.com/ciscozone0464.html
http://cisco21.iquebec.com/ciscozone0724.html
http://cisco21.iquebec.com/ciscozone0212.html
http://cisco21.iquebec.com/ciscozone0345.html
http://cisco21.iquebec.com/ciscozone0778.html
http://cisco21.iquebec.com/ciscozone0796.html
http://cisco21.iquebec.com/ciscozone0037.html
http://cisco21.iquebec.com/ciscozone0158.html
http://cisco21.iquebec.com/ciscozone0690.html
http://cisco21.iquebec.com/ciscozone0021.html
http://cisco21.iquebec.com/ciscozone0477.html
http://cisco21.iquebec.com/ciscozone0543.html
http://cisco21.iquebec.com/ciscozone0748.html
http://cisco21.iquebec.com/ciscozone0582.html
http://cisco21.iquebec.com/ciscozone0786.html
http://cisco21.iquebec.com/ciscozone0115.html
http://cisco21.iquebec.com/ciscozone0337.html
http://cisco21.iquebec.com/ciscozone0766.html
http://cisco21.iquebec.com/ciscozone0944.html
http://cisco21.iquebec.com/ciscozone0683.html
http://cisco21.iquebec.com/ciscozone0577.html
http://cisco21.iquebec.com/ciscozone0965.html
http://cisco21.iquebec.com/ciscozone0273.html
http://cisco21.iquebec.com/ciscozone0956.html
http://cisco21.iquebec.com/ciscozone0843.html