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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T12:41:12+00:00 2026-05-26T12:41:12+00:00

I have a master class: A with a constructor that takes an optional login

  • 0

I have a master class: “A” with a constructor that takes an optional login and password for a web service. I have subclasses of A: A1, A2 and A3 that have methods for other web services from the same company. They all use the same login and password, but have different uses. Each has it’s own set of methods.

So, if I already have an instance of the parent class (or any of the sub-classes), how can I create the other sub-classes without having to reauthenticate the parent class?

What I want to do is like this:

class A {

    protected static $authenticated_service_handle; //Takes a while to set up
    protected $instance_of_A1;

    function __construct($login = null, $password = null) {
        //Go do login and set up
        $authenticated_service_handle = $this->DoLogin($login, $password)

        //HELP HERE: How do I set up $this->instance_of_A1 without having to go through construction and login AGAIN??
        //So someone can call $instance_of_A->instance_of_A1->A1_Specific_function() ?
    }

}

class A1 extends A {

    function __construct($login = null, $password = null) {
        parent::__construct($login, $password);
    }

    public function A1_Specific_function() {
    }

}

//How I want to use it.
$my_A = new A('login', 'password');
$method_results = $my_A->instance_of_A1->A1_Specific_function();
$second_results = $ma_A->instance_of_A2->A2_Specific_function();

Any ideas in how to do this naturally? It seems kind of backwards from standard OO methodology but my calling clients will need to use methods of A1, A2 and A3 at the same time but the number of methods and organization of them lend themselves to breaking into subclasses based on functionality.

  • 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-26T12:41:12+00:00Added an answer on May 26, 2026 at 12:41 pm

    If the thing you create in your class A is a connection which could be used by all classes that require it, you could use it as such:

    class ServiceConnection
    {
        private $_authenticated_service_handle; //Takes a while to set up
    
        function __construct($login = null, $password = null) {
            //Go do login and set up
            $_authenticated_service_handle = $this->DoLogin($login, $password)
        }
    
        public function DoSomething()
        {
            $_authenticated_service_handle->DoSomething();
        }
    
    }
    

    And pass that connection to all objects that need it:

    $connection = new ServiceConnection('login', 'password');
    
    $my_A1 = new A1($connection);
    $my_A2 = new A2($connection);
    $my_A3 = new A3($connection);
    
    $my_A1->A1_Specific_function();
    $my_A2->A2_Specific_function();
    $my_A3->A3_Specific_function();
    

    The AN-classes will look like this:

    class A1 {
    
        private $_connection;
    
        function __construct($connection) {
            $_connection = $connection;
        }
    
        public function A1_Specific_function() {
            $_connection->doSomething();
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a master page that contains an ASP.NET server side Menu control (System.Web.UI.WebControls.Menu)
I have a master branch in my project, that I use to pull changes
I have a view that is strongly typed: <%@ Page Title= Language=C# MasterPageFile=~/Views/Shared/Site.Master Inherits=System.Web.Mvc.ViewPage<MPKwithMVC.Models.SmartFormViewModel>
I currently have a abstract controller class that I all my controllers inherit from.
I have a class with overloaded constructor (C#) It can be initialized in few
I have a class Foo<T, U> with the following constructor: public Foo() { clazz
One challenge using hibernate is that manged classes have to have a default constructor
I have a class that looks roughly like this: template<std::size_t dim> class Foo {
I have a class with an implicit const char * constructor. My intuition tells
I have a base class that three classes inherit from. Whenever these child classes

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.