Solved

Zend Framework objects vs arrays

Posted on 2013-05-16
3
368 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 34

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 110

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
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.…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

728 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