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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T20:04:20+00:00 2026-05-30T20:04:20+00:00

I have the following setup: abstract class AParentLy{ private $a; private $b; function foo(){

  • 0

I have the following setup:

abstract class AParentLy{
   private $a;
   private $b;

   function foo(){
      foreach(get_class_vars(get_called_class()) as $name => $value){
         echo "$name holds $value";
      }
   }
}

class ChildClass extends AParentLy{
   protected $c='c';
   protected $d='d';
}

$object = new ChildClass();
$object->foo();

What I want it to output is:

c holds c
d holds d

What it does output is:

c holds c
d holds d
a holds
b holds

The get_called_class() method correctly outputs “ChildClass”

I’m fairly new to class inheritance in PHP and from what I can gather the problem lies somewhere in the so scope. But I can’t figure out how to make it work.

(The somewhat questionable solution would be to just go ahead and add a great big if($name!=’a’ && $name!=’b’ …~ into the mix. But I’m sure there must be another, more sane and stable way to do this)

  • 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-30T20:04:21+00:00Added an answer on May 30, 2026 at 8:04 pm

    Had another go at the whole experiment this question was a part of.

    The eventual solution (just in case anyone stumbles over this and has the same problem) I came to was to create the following method within the parent class:

    function get_properties(){
        foreach(get_class_vars(get_called_class()) as $name => $value){
            if(!in_array($name,array_keys(get_class_vars('Parent')))){
                $r[$name]=$this->$name;
            }   
        }
        return $r;
    }
    

    with this you get every parameter (and their value) of the child class without any of the parent class. You’ll have to change the function a bit if you ever change the class name, but at least for me this was a workable solution.

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

Sidebar

Related Questions

I have the following code: abstract class BaseLpmServiceForm extends BaseFormPropel { public function setup()
I have following setup A -> B -> C A-Mapping: <hibernate-mapping> <class name=db.base.A table=A>
I have the following setup (simplified, obviously): An abstract class, with an A object
I have the following snippet of code. abstract class MrParent { public function __construct()
I want to have the following setup: abstract class Parent { public static String
I have following setup. from django.db import models from django.contrib.auth.models import User class Event(models.Model):
i have following model setup class Category < ActiveRecord::Base has_ancestry :cache_depth => true, :depth_cache_column
I have the following class: public abstract class AbstractParent { static String method() {
I have the following code: public abstract class BaseController : Controller { public IUserService
I have the following set up: public abstract class A { public void f()

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.