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

  • Home
  • SEARCH
  • 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 3341748
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T00:45:30+00:00 2026-05-18T00:45:30+00:00

Basically what I’m trying to achieve is to make the object reference another object

  • 0

Basically what I’m trying to achieve is to make the object reference another object of the same class – from inside of a method. It’d be perfect if the following would work:

$this = new self;

But one cannot reassign $this in php.

I of course know, I can return another object from the method and use that, so please do not advise that. The question is:

How can I make $this to be a clone of another object of the same class?

or more specifically,

I want an object to revert to a specific state that was previously saved.


EDIT: some examples where this might be useful.

Say you have an Url object that accepts controller, action and lots more things. You are going to have it fetch a lot of links with the same, say, controller and action, but other properties will differ. I instantiate the object with the common parameters, call a method for it to save its state which it reverts to after outputting a link (IOW after __toString method).

$defaultPath = Url::factory('controller','action1')->extraParams('status',0)->save();

echo $defaultPath->action('action2'); // this has action2 as action and a status 0 as extra params
echo $defaultPath->extraParams('status',2); // this has action1 as action and 2 as status

Another use is I have a CRUD table and I have to configure each column as an object I pass to the main table object. After passing the column I launch a reset method on the column, so I can have code like the following:

    $column->field = 'layouts_id';
    $column->value = $layoutId;
    $dbInput->addColumn($column);

    $column->field = 'pages_id';
    $column->value = $pagesId;
    $dbInput->addColumn($column);

In both situations I save a lot of code and confusion, don’t you think?

  • 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-18T00:45:31+00:00Added an answer on May 18, 2026 at 12:45 am

    I see a huge problem with what you’re intending:

    If you found a way to reassign $this, how do you make sure, that anybody using your class knows how it behaves? While I admit, that it’s kinda interesting to do

    $column->field = 'layouts_id';
    $column->value = $layoutId;
    $dbInput->addColumn($column);
    
    $column->field = 'pages_id';
    $column->value = $pagesId;
    $dbInput->addColumn($column);
    

    What happens, if I need $column twice?

    $column->field = 'layouts_id';
    $column->value = $layoutId;
    $dbInput->addColumn($column);
    // log my value
    $logger->log($column); // oops, it's empty
    
    ...
    

    You would break the expected behaviour and imho make it extremely hard to grasp your code.


    But to give some ideas on how to still achieve the aforementioned behaviour:

    // clone when passing on
    $column->field = 'value_id';
    $column->value = $value;
    $dbInput->addColumn(clone $column);
    
    // manually (re-)create a new object based on an older
    $column->field = 'value_id';
    $column->value = $value;
    $dbInput->addColumn(new Column($column));
    

    Otherwise the reset() method also sounds feasible:

    class Column() {
    
        public function reset() {
            $this->field = 'standard field id';
            $this->value = 'standard field value';
        }
    
        public function __construct() {
            $this->reset();
        }
    
    }
    

    This way you could use it like so:

    // manually (re-)create a new object based on an older
    $column->field = 'value_id';
    $column->value = $value;
    $dbInput->addColumn($column);
    $column->reset();
    

    To overcome the problem of specifying the default values twice (untested):

    class Column() {
    
        public $field = 'standard field id';
        public $value = 'standard field value';
    
        // keep a static object for resetting
        private static $__blueprint = null;
    
        public function reset() {
            foreach (self :: $__blueprint as $k => $v)
                $this->$k = $v;
        }
    
        public function __construct() {
            if (!isset(self :: $__blueprint))
                self :: $__blueprint = clone $this;
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically I am trying to restart a service from a php web page. Here
Basically I'm trying to accomplish the same thing that mailto:bgates@microsoft.com does in Internet Explorer
Basically, I'm trying to tap into the Soap pipeline in .NET 2.0 - I
Basically, what I'm trying to create is a page of div tags, each has
Basically, I have a matrix class like this (with a lot of operator overloads
Basically, I have an array, let's say @badvalues . I have another array, let's
Basically what I want to do it this: a pdb file contains a location
Basically, something better than this: <input type=file name=myfile size=50> First of all, the browse
Basically I have some code to check a specific directory to see if an
Basically I'm going to go a bit broad here and ask a few questions

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.