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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T11:49:51+00:00 2026-05-23T11:49:51+00:00

I understand the concept of abstract class, but I saw a code in a

  • 0

I understand the concept of abstract class, but I saw a code in a book that I do not understand.

I will short the code and would like you help me to understand, I know what it does I just don’t why is working.

Here I declared and abstract class DataObject and its contructor

abstract class DataObject {
    protected $data = array();

    public function __construct( $data ){
        foreach ( $data as $key => $value )
        {
            if( array_key_exists( $key, $this->data )) 
                $this->data[$key] = $value;
        }       
    }
}

Then I have this

class Member extends DataObject {
    protected $data = array(
                        "username" => "",
                        "password" => ""
    );

    public function getInfo(){
        echo "Usernarme: " . $this->data["username"] . " <br/>password: " . $this->data["password"];
    }
}

So when I do this

$m= new Member( array( 
                    "username" => "User",
                    "password" => "Some password" )
                );
$m->getInfo();

I get

Usernarme: User
password: Some password

To be more specific.

  • Looks like since I did not create a constructor for the extended class is calling implicitly the father class, right?

  • How the constructor works in a way that it is validating the data array according to the Member array values?, I mean if when I create the Object

$m= new Member( array(
“username” => “User”,
“password” => “Some password” )
);

Change the key “username” for “usernames” it won’t assign the value “User” for example.

Thanks

  • 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-23T11:49:52+00:00Added an answer on May 23, 2026 at 11:49 am

    In the parent constructor:

    if( array_key_exists( $key, $this->data )) 
        $this->data[$key] = $value;
    

    This prevents it from creating new keys. In the child class the keys “username” and “password” are defined, so those are the only keys that the constructor will allow to be written.

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

Sidebar

Related Questions

I have in my code the concept of command : public abstract class BaseCommand
I completely understand the concept of expression trees, but I am having a hard
I need some help in understanding a python concept. class TilePuzzleProblem(search.Problem): This class is
Is there a reason to use a 100% abstract class and not an interface
I understand the concept of third-party cookies, but don't understand by what mechanism they
I understand the concept behind DI, but I'm just learning what different IoC containers
I'm trying to understand this concept but I don't get it yet. I'm new
I think I understand the concept of virtual methods and vtables, but I don't
Could somebody please help me understand the concept of 'impersonation'? The way I understand
I understand the concept of database normalization, but always have a hard time explaining

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.