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 6700725
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T06:50:57+00:00 2026-05-26T06:50:57+00:00

In PHP, if you make an array of objects, are the object methods (not

  • 0

In PHP, if you make an array of objects, are the object methods (not data members) copied for each instance of the object in the array, or only once? I would assume that for memory reasons, the latter is true; I just wanted to confirm with the StackOverflow community that this is true.

For example, suppose I have a class MyClass with a couple of methods, i.e.

class MyClass {
    public $data1;
    private $data2;
    public function MyClass($d1, $d2) { 
        $this->data1=$d1;   $this->data2=$d2;
    }
    public function method1() {  }
    public function method2() {  }
}

Obviously in reality method1() and method2() are not empty functions.
Now suppose I create an array of these objects:

$arr = array();
$arr[0] = & new MyClass(1,2);
$arr[1] = & new MyClass(3,4);
$arr[2] = & new MyClass(5,6);

Thus PHP is storing three sets of data members in memory, for each of the three object instances. My question is, does PHP also store copies of method1() and method2() (and the constructor), 3 times, for each of the 3 elements of $arr? I’m trying to decide whether an array of ~200 objects would be too memory-intensive, because of having to store 200 copies of each method in memory.

Thanks for your time.

  • 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-05-26T06:50:58+00:00Added an answer on May 26, 2026 at 6:50 am

    By definition (and that is your code), a function only exists once. That’s why you produce code (and not data).

    However, you can then use your code to produce a lot of data. But that’s another story ;).

    So unless you do not needlessly duplicate code across objects, your function(s) will only exist once. Independent how many instances of the code you create. Only the data associated to the code (the class members) are duplicated.

    Sounds fair?

    BTW:

    $arr[0] = & new MyClass(1,2);
    

    Gives you a strict standards error. You can not assign a reference/alias with the new keyword. Probably this way of writing is influenced by PHP 4 code, but this has changed since PHP 5 which introduced the object store.

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

Sidebar

Related Questions

Is there a way to make a read-only property of an object in PHP?
Is there a way in PHP to make HTTP calls and not wait for
i would like to learn how to make PHP/CSS syntax generator. i assume this
With PHP, I can make numerous PHP websites locally and then upload each of
I would like to make a php script that can capture a page from
I'm trying to make a class which has methods called using PHP's __call() magic
I'm using curl to make php send an http request to some website somewhere
Is it possible to make php variables public and accessible anywhere on the timeline?
Is there any function in PHP to make the first character of the word
I'm trying to make a PHP script, I have the script finished but it

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.