Solved

Zend Framework objects vs arrays

Posted on 2013-05-16
3
362 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 108

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

Free camera licenses with purchase of My Cloud NAS

Milestone Arcus software is compatible with thousands of industry-leading cameras for added flexibility. Upon installation on your My Cloud NAS, you will receive two (2) camera licenses already enabled in the software. And for a limited time, get additional camera licenses FREE.

Question has a verified solution.

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

There is a huge demand for CodeIgniter among the PHP web developers due to its dynamic features and benefits these days. It is one of most popular and agile open source PHP framework for creating robust web applications in PHP web development field.…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The purpose of this video is to demonstrate how to set up the WordPress backend so that each page automatically generates a Mailchimp signup form in the sidebar. This will be demonstrated using a Windows 8 PC. Tools Used are Photoshop, Awesome…
The viewer will learn how to dynamically set the form action using jQuery.

943 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now