Solved

Zend Framework objects vs arrays

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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

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…
Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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…

746 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

9 Experts available now in Live!

Get 1:1 Help Now