Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9015759
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T03:49:49+00:00 2026-06-16T03:49:49+00:00

Would this method foreach($data as &$d) $obj[]=ClassName::createObject($data); or foreach($data as &$d){ $obj[] = new

  • 0

Would this method

    foreach($data as &$d)
       $obj[]=ClassName::createObject($data);

or

    foreach($data as &$d){
       $obj[] = new ClassName;
       $obj[end($obj)]->loadData($data);
     }

–

     class ClassName{
        public static function createObject($data){
              $obj = new ClassName;
              //do stuff with $data
              return $obj;
        }

        public function loadData($data){
              //do stuff with $data;

        }

     }

The ::createObject method makes for code which is a lot less painful to read while the second method doesn’t have to return huge objects.
I’m not sure how variables are handled in a language like PHP so is there a big difference in performance?
Which method would be best to use?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T03:49:51+00:00Added an answer on June 16, 2026 at 3:49 am

    First looks ok. You don’t need to pass $d by reference in this case. And probably you want ClassName::createObject($d). Still I’d refactor it to:

    foreach($data as $d) {
       $obj[]=new ClassName($d);
    }
    

    This is just ugly, not for performance reasons:

    foreach($data as &$d){
       $obj[] = new ClassName;
       $obj[end($obj)]->loadData($data);
     }
    

    Instead you could do this:

    foreach($data as &$d){
       $object = new ClassName;
       $object->loadData($d);
       $obj[] = $object;
     }
    

    If the data is essential for the object – set it from the construct. If the data is optional, then the loadData method makes sense. But if you want to move the construct logic to a static method – this is pointless. No performance advantage.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In Java, a 'static method' would look like this: class MyUtils { . .
In my base class I have a generic method (ideally this would be a
I was wondering what the parameters from this method would return. - (void) observeValueForKeyPath:(NSString
I would like to know if this method of setting up routing in Zend
Is there builtin method that would do this or do I always have to
in iPhone, this would be simple---each view has a scrollViewDidScroll method. I am trying
This is an objective-c question. I would like to call a method in an
Why would this happen? You start up a thread, and it runs a class.
Would this be a proper way to dispose of a BackGroundWorker? I'm not sure
Would this be the correct way of declaring that an XML element Cluster contains

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.