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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T11:56:44+00:00 2026-05-12T11:56:44+00:00

Say you have two classes, A and B. Is it possible to instantiate both

  • 0

Say you have two classes, A and B. Is it possible to instantiate both classes once and then let class B call methods in class A, and vice versa?

It can be done using double colon (::) … … but then the method becomes static – is that a disadvantage? (see example below)

Can it be done in other ways? With interfaces?

This code shows what I try to do:

class A {
  function horse() {
    echo "horse";
  }
}

class B {
  function jump() {
    // $A = new A; ... don't want to add this in each method.
    $A->horse();  // Fails - $A is out of scope ($A = new A;).
    // A::horse();  // Old code style - works.
    // $this->horse();  // Works if you extend A - not self-documenting.
    // $this->A->horse();  // Fails - out of scope.
  }
}

$A = new A;
$B = new B; // Better to use "$B = new B($A);" ?
$B->jump(); // fails - the horse is sleeping.

Edit

Well, I am building a MVC-framework and I want to re-use code from other classes.
Some real-world examples:

  • a database object that is being passed across classes.
  • a “url” class that creates/manipulates URLs – used by other classes.
  • … and a code example:

    class url {
        function anchor($url,$name) {
            return "<a href="{$url}\">{$name}</a>";
        }
    }
    
    class someclass {
        function text($str,$url) {
            return "{$str}. " . $url->anchor($url,"Read more...");
        }
    }
    
  • 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-12T11:56:44+00:00Added an answer on May 12, 2026 at 11:56 am

    I think what you are asking for is multiple inheritance where you could extend both A and B like this

    <?php
    class C extends A,B {
    
    //...
    
    }
    

    This however is not possible in PHP for good reasons(it actually is creating more problems than it’s trying to solve).

    Now you might ask yourself if there is any alternative to multiple inheritance and the answer is: Yes, there is! Have a look at the strategy pattern(as Benjamin Ortuzar also has pointed out).

    UPDATE:

    I just read your question a second time and figured that you might be looking for the singleton pattern, which lets you instantiate an instance of an class only once like this:

    class A
    {
    
        protected static $_instance;
    
        protected function __construct() //prohibit creating instances from outside
        { }
    
        public static function getInstance() 
        {
          if( self::$_instance === NULL ) {
            self::$_instance = new self();
          }
          return self::$_instance;
        }
    }
    
    $instance = A::getInstance();
    

    Now A::getInstance() always returns the same instance of A which you can use in B and you can have both the advantages of dynamic functions and the accessibility of static functions.

    UPDATE2:

    Your database belongs into a registry if you can have more than one db-connection. If you’re absolutely certain that you will always need only one db-connection you could as well make it a singleton.

    For the URL helper I’d suggest writing a static class if you can and if you really need it to be dynamic make it a singleton, as mentioned before.

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

Sidebar

Related Questions

Let's say that I have two classes A and B. class A { private:
Say I have a div that uses two css classes that both use text-align,
Say I have two classes like this: class A{ private static Random random =
Is it possible to have a single class reside within two name-spaces and how
I have the following code: def f(): class XYZ: # ... cls = type('XXX',
I'm trying to understand the use of the keyword self Let's say that I
I have a maven project which uses the shade plugin to create a jar
I wonder what fits better into the CSS philosophy: CSS classes mark entities -
I wanted to make a special version of shared_ptr that would perform specific operations
Rephrased Question I found that my original question wasn't clear enough and the repliers

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.