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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T15:56:51+00:00 2026-06-17T15:56:51+00:00

I have two classes involving composition not inheritance., class A and class B. One

  • 0

I have two classes involving composition not inheritance., class A and class B. One of class A’s properties is an array of class B objects. Class A has a public method A::getName(). Class B also has a public method with the same name. The method for class A is as follows:

public function getName()
{
    return $this->_name;
}

My problem is when I’m in Class B and I try to access this public method for class A, I get the “cannot access protected property” error. $_name is a private property in each class. Class A’s would be the name, for example, of a form, and for B, the name of the field.

This is the code that generates the error (constructor for class B):

public function __construct($name)
{
    $this->foo = A::getName() .'-'. $name;
}

Why is it not allowing me to access class A’s public method getName()? Any way to fix or get around this?


FIX:

I realized I was invoking class A’s method statically, though I need to deal with each object individually, as each object has a unique name. To solve my issue I gave a public set function for class B for the unique name, and called that in class A:

class A
{
    ...

    $this->list[$B_name] = new B($B_name);

    $this->list[$B_name]->setID($this->_name, $B_name);

}

class B
{
    ...

    public function setID($name, $subName)
    {
        $this->foo = $name .'-'. $subName;
}
  • 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-17T15:56:52+00:00Added an answer on June 17, 2026 at 3:56 pm

    This is because of you are trying to call it statically and in that method you are accessing instance variable.

    it could work like this:

    class A {
       private static $_name = "A";
       public static function getName() {
            return self::$_name;
       }
    }
    
    class B {
       public function __construct($name) {
            $this->foo = A::getName() .'-'. $name;
       }
    }
    

    or this way (this is imho your situation)

    class A {
       private $_name;
    
       public function __construct($name) {
            $this->_name = $name;
       }
    
       public function getName() {
           return $this->_name;
       }
    }
    
    class B {
       public function __construct($name, A $parent) {
            $this->foo = $parent->getName() .'-'. $name;
       }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two classes: Action class, that has a method for executing VBScript files,
I have two classes and a method like this: public class Class1 { public
I have two classes (MVC view model) which inherits from one abstract base class.
I have two classes, one that inherits from the other. The base class is
I have two classes, class A and class B. Class A has a mission
I have two classes: class A { public: int i; }; class B :
I have two classes as follows: public class Info { [XmlAttribute] public string language;
I have two classes: public class MyBase { public virtual void DoMe() { }
I have two classes defined like public class PostleitzahlList : ObservableCollection<Postleitzahl> { } public
I have two classes declared in C# code: public class A { public static

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.