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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T20:40:15+00:00 2026-05-25T20:40:15+00:00

I have the following three classes: class a { public $test; } class b

  • 0

I have the following three classes:

class a
{ public $test; }

class b extends a { }
class c extends a
{
    function return_instance_of_b() { }
}

As you can see, both classes b and c derive from a. In the return_instance_of_b() function in c, I want to return an instance of the class b. Basically return new b(); with one additional restriction:

I need the data from the base class (a) to be copied into the instance of b that is returned. How would I go about doing that? Perhaps some variant of the clone keyword?

  • 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-25T20:40:16+00:00Added an answer on May 25, 2026 at 8:40 pm

    You can use the get_class_vars function to retrieve the names of the variables you want to copy, and just loop to copy them.

    The variables that are defined are protected so they are visible to get_class_vars in its scope (since c extends a), but not directly accessible outside the class. You can change them to public, but private will hide those variables from get_class_vars.

    <?php
    class a
    { 
        protected $var1;
        protected $var2;
    }
    
    class b extends a 
    {
    }
    
    class c extends a
    {
        function __construct()
        {
            $this->var1 = "Test";
            $this->var2 = "Data";
        }
        
        function return_instance_of_b() 
        {
            $b = new b();
            // Note: get_class_vars is scope-dependant - It will not return variables not visible in the current scope
            foreach( get_class_vars( 'a') as $name => $value) {
                $b->$name = $this->$name;
            }
            return $b;
        }
    }
    
    $c = new c();
    $b = $c->return_instance_of_b();
    var_dump( $b); // $b->var1 = "Test", $b->var2 = "Data
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following three classes: public class Student { private Integer studentId; private
Suppose I have the following classes: public class Test { public static void main(String[]
I have the following three classes public class Base { string name; } public
Suppose I have two classes defined the following way: public class A { public
I have three different base classes: class BaseA { public: virtual int foo() =
Have the following XML model <A></A> <B></B> <C></C> <D></D> Also have three classes class
I have the following classes: public class MyClass { public void MyMethod() { try
I have following two classes: template <size_t size> class Cont{ public: char charArray[size]; };
Let's say I have the following two classes: public class Person { public string
I have the following problem. I have three classes, A, B and C. A

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.