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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T02:20:58+00:00 2026-05-17T02:20:58+00:00

What would be a good way (along with any pros and cons) of initializing

  • 0

What would be a good way (along with any pros and cons) of initializing an instance of a PHP class with another object of the same class (ideally in PHP 4.x)?

Here in initialize() is essentially what I’d like to be able to do (example is extremely simplified from my use-case, see below):

$product = new Product('Widget');
$product2 = new Product('Widget #2');
$product->initialize($product2);
echo $product->name;  // echos "Widget #2"

class Product {
 var $name;
 function __constructor($name) {
   $this->name = $name;
 }
 function initialize($product) {
   // I know this cannot be done this way in PHP. 
   // What are the alternatives and their pros & cons?
   $this = $product;  
 }
}

I know this may not be "good programming practice"; with 20+ years programming experience on other languages I know a bit about what’s good and what’s not. So hopefully we won’t get hung up on if doing this makes sense or not. I have a use-case working with some open-source code that I can’t change so please just bear with me on my need for it. I’m actually trying to create an OOP wrapper around some really ugly array code buried deep in the core of WordPress.

I’m trying to write it so in future versions they can move away from the ugly array-based code because everyone will be using the new API that otherwise fully encapsulated these nasty arrays. But to make it work elegantly I need to be able to do the above (in PHP 4.x) and I don’t want to write code that just copies the properties.

Thanks in advance for your help.

UPDATE

Many of you are suggesting clone but unless I misunderstand that doesn’t address the question. clone makes a copy; that’s not the crux of the question. I’m instead trying to get the constructed object to "become" the object passed in. At this point I’m assuming there isn’t a way to do that based on the fact that 0 out of 5 answers have suggested anything but I’ll wait a bit longer before selecting a best in case it was simply that my questions was unclear.

  • 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-17T02:20:59+00:00Added an answer on May 17, 2026 at 2:20 am

    In PHP 5, object cloning might be more relevant:

    http://php.net/manual/en/language.oop5.cloning.php

    You can define a special __clone method.

    In PHP 4 and 5, you can copy properties via:

    function copy($obj)
    {
      foreach (get_object_vars($obj) as $key => $val)
      {
         $this->$key = $val;
      }
    }
    

    However, you wrote “I don’t want to write code that just copies the properties,” and I’m not exactly sure what you mean by that.

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

Sidebar

Related Questions

What would be a good way to determine if a string contains an IPv4
What would be a good way to writein C# a .NET3.5 Thread Safe DataSource
Wondering what the best / good way of doing this would be in jQuery.
What is a good way to represent finite automaton in Haskell? How would the
Question What would be a good (ideally, technical) reason to ever program some non-trivial
What is a good way of passing an object as a parameter? In my
Is there any way I can have thread-safe coroutines along with boost::asio? I want
I've decided that it would be good for me to move outside of my
I have some useful wpf buttons to test some functionality. It would be good
Two good examples would be google and facebook . I have lately pondered the

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.