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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T13:52:07+00:00 2026-05-25T13:52:07+00:00

I would like to have my abstract parent class have a method that would

  • 0

I would like to have my abstract parent class have a method that would be inherited by a subclass which would allow that subclass to iterate through all of it’s variables (both the variables inherited from the parent, and it’s own variables).

At the moment if I implement this method in the parent, then only the parent’s variables will be iterated over:

class MyObject {

    private $one;
    private $two;
    private $three;

    function assignToMembers() {
        $xx = 1;
        foreach($this as $key => $value) {
            echo "key: ".$key."<br />";
            $this->$key = $xx;
            $xx++;
        }
    }

    public function getOne() {
        return $this->one;
    }

    public function getTwo() {
        return $this->two;
    }

    public function getThree() {
        return $this->three;
    }
}

class MyObjectSubclass extends MyObject {

    private $four;
    private $five;

    public function getFour() {
        return $this->four;
    }

    public function getFive() {
        return $this->five;
    }
}
$o = new MyObjectSubclass();
$o->assignToMembers();
echo $o->getOne()." ";
echo $o->getTwo()." ";
echo $o->getThree()." ";
echo $o->getFour()." ";
echo $o->getFive()." ";

// This prints 1 2 3

On the other hand, if I put the assignToMembers function in the subclass, then only the subclass’s members are iterated over.

Because I want my assignToMembers() function to be usable by a number of subclasses, I don’t want to have to implement it in every one, only in the parent class, but it looks like I will have to unless it can access that class’s members.

Is there any way to acheive this?

Thanks in advance.

  • 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-25T13:52:08+00:00Added an answer on May 25, 2026 at 1:52 pm

    You’ll need to use protected if you want your code to work as described. Remember the roles of the access/visibility modifiers:

    • private – class level only access
    • protected – whole inheritance chain access
    • public – universal access
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an abstract parent class, and I would like it to force all
If I have a parent-child that defines some method .foo() like this: class Parent
I have an abstract class which I would like to be able to expose
I have an abstract class and I would like to use it quickly by
Let's say I have an abstract parent class called shape, and that there are
In my project, I have an abstract base class Base. I would like to
I have an abstract class and would like to add a static dictionary for
I have a problem that I would like have solved via a SQL query.
I would like to have all developers on my team to use the same
I would like to have information about the icons which are displayed alongside the

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.