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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T00:42:40+00:00 2026-06-05T00:42:40+00:00

I have one class that creates an instance of another class. Is it possible

  • 0

I have one class that creates an instance of another class. Is it possible for the second class to retrieve parameters from the calling class.

For example

Class_A {
    protected $myArray = array('item 1', 'item2', 'item3')

    public function __construct()
    {
        $nextClass = new ClassB();
        echo $nextClass->countArrayItems();
    }
}

Class_B {
    public function countArrayItems()
    {
        return count(Class_A->myArray);
    }
}

$newClass = new Class_A;
  • 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-06-05T00:42:43+00:00Added an answer on June 5, 2026 at 12:42 am

    You could pass a reference to your Class_A instance to your Class_B constructor:

    Class_A {
        public $myArray = array('item 1', 'item2', 'item3')
    
        public function __construct()
        {
            $nextClass = new Class_B($this);
            echo $nextClass->countArrayItems();
        }
    }
    
    Class_B {
        public function __construct($classA_instance) {
            $this->classA_instance = $classA_instance;
        }
        public function countArrayItems()
        {
            return count($this->classA_instance->myArray);
        }
    }
    
    $newClass = new Class_A;
    

    (as noted by @chris, Class_A->myArray needs to be public)

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

Sidebar

Related Questions

I have one abstract class named contact and another class called client that inherits
I have a large-ish project that contains one class that reads from a file
I have one grails application.In that I have one model class named Book. From
I'm creating a class that will have one public method, which returns a value
I'm using Java. I want to have a setter method of one class that
I have a class that contains a boolean field like this one: public class
I have a SharedPreferences that currently works in one class but doesn't work in
I have a class that builds my gui. One part of the gui is
I have a class that is mapped in fluent nhibernate but I want one
I have a class that instantiates two classes which implement interfaces. I want one

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.