Solved

Zend Framework objects vs arrays

Posted on 2013-05-16
3
364 Views
Last Modified: 2013-06-01
Hi

I am working on a project that is written in Zend Framework. In that, the entire objects being given to views. For example


$Object = new ClassName();

$this->view->data = $Object;

Open in new window

                       



then within the view, it pull the necessary info from the object.

My question is, is this is a good practice in general ? I think passing only necessary info using array or individual variables to views is more efficient than completely duplicating the object in the memory. But, I might be wrong, because processing to get the info what we need only within the view might be expensive. I just want to know what is the best practice for this.

Any suggestions and reading materials (links) will be greatly appreciate it.

Thanks
Peiris
0
Comment
Question by:Peiris
3 Comments
 
LVL 33

Accepted Solution

by:
Slick812 earned 300 total points
ID: 39175445
Greetings   Peiris, ,  I will give my opinion about this, , you ask = " passing only necessary info using array or individual variables to views is more efficient than completely duplicating the object in the memory"    and you give this code as example -
$Object = new ClassName();
$this->view->data = $Object;
If you look it up in the PHP manual, it tells you that for objects (unlike arrays) when you use an equal operator = for an Object, , the Object is NOT " duplicated in the memory", so the = operator only creates a "php reference" to the first object, not a new and different object. So any class method or property op on $this->view->data  , , will actually be doing it on the first object $Object.

I have found that it can be very effective to have PHP reference variables in a CHILD class like view in  $this->view, that go back to existing Objects and arrays in the top or parent class of the child or sub class, This way any changes done to these variables, by the child class OR the top class, are available to either object without calling extra class methods.
Also it can save on memory duplication and access scope, if you use references back to the top object in child objects.
You ask, "I just want to know what is the best practice for this", , and that depends on what you need and the process to do to get there, but if you do not use or do not know the PHP "reference" variable use and methods, then you may start to use those, I has helped me very much in using child objects like $this->view.

ask questions if I did not explain this clearly.
0
 
LVL 109

Assisted Solution

by:Ray Paseur
Ray Paseur earned 200 total points
ID: 39175975
Objects are not duplicated.  New assignments become pointers to the original object.  This is called "by reference" and yes, it is a best-practice.  If you want a new instance of the class, you must instantiate a new object.  You can also clone objects, if you really want another data element.
http://php.net/manual/en/language.oop5.references.php

Best regards, ~Ray
0
 

Author Closing Comment

by:Peiris
ID: 39212951
This solved my problem
0

Featured Post

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
google map tracking website 5 23
javascript: add id amounts 5 47
comma true 6 33
how to use a switch statement with heredoc 11 23
Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question