Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Zend Framework objects vs arrays

Posted on 2013-05-16
3
Medium Priority
?
385 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 1200 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 111

Assisted Solution

by:Ray Paseur
Ray Paseur earned 800 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

Tech or Treat!

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

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…
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…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
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…
Suggested Courses

610 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