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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T03:19:26+00:00 2026-06-18T03:19:26+00:00

I’m a newbie in OOP in PHP. I have the following problem: I have

  • 0

I’m a newbie in OOP in PHP. I have the following problem:
I have 1 parent class called ‘User’ and 2 subclasses ‘Business’ and ‘Standaard’.

My parent class looks something like this:

class User {    
protected $m_iId;
protected $m_iFoto;
protected $m_sEmail;

/*Constructor*/
public function __construct(){
    $this->Id = -1;
    $this->Foto = "";
    $this->Email = "";
} 

/*Setter*/                    
public function __set($p_sProperty, $p_sValue){
    switch($p_sProperty){
        case "Id":
            if(is_numeric($p_sValue) && $p_sValue !== -1){
                $iNumber = intVal($p_sValue);
                if($iNumber >= -1){
                    $this->m_iId = $p_sValue;
                }
                else{
                    echo("Not a valid id: ".$p_sValue);
                }
            }
            break;
        case "Foto":
            if(is_numeric($p_sValue) && $p_sValue !== -1){
                $iNumber = intVal($p_sValue);
                if($iNumber >= -1){
                    $this->m_iFoto = $p_sValue;
                }
                else{
                    echo("Not a valid Foto_id: ".$p_sValue);
                }
            }
            break;
        case "Email":
            $this->m_sEmail = $p_sValue;
            break;  
        default: echo("Unknown property: ".$p_sProperty);
    }
}

/*Getter*/
public function __get($p_sProperty){
    $vResult = null;
    switch($p_sProperty){
        case "Id";
            $vResult = $this->m_iId; 
            break;
        case "Foto";
            $vResult = $this->m_iFoto; 
            break;
        case "Email";
            $vResult = $this->m_sEmail; 
            break;
        default: echo("Unknown property: ".$p_sProperty);
    }
    return $vResult;
}

}

A child child looks likes this:

class UserStandaard extends User {

    //velden die bereikbaar zijn
    protected $m_sName;

    /*Constructor*/
    public function __construct(){
        parent::__construct();
        $this->Name = "";
    } 

    /*Setter*/                    
    public function __set($p_sProperty, $p_sValue){
        switch($p_sProperty){
            case "Name":
                $this->m_sName = $p_sValue;
                break;
            default: echo("Unknown property: ".$p_sProperty);
        }
    }

    /*Getter*/
    public function __get($p_sProperty){
        $vResult = null;
        switch($p_sProperty){
            case "Name";
                $vResult = $this->m_sName; 
                break;
            default: echo("Unknown property: ".$p_sProperty);
        }
        return $vResult;
        }
}

My question is the following:
How can I request the Id?
I want to do something like:

$oUser = new UserStandaard();
echo $oUser->Id;

But that doesn’t work… It keeps echoing

Unknown property: Id

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-06-18T03:19:28+00:00Added an answer on June 18, 2026 at 3:19 am

    The problem is that your derived class implementation of __get and __set do not call the base class implementation. This does not happen automatically, and you need to do it explicitly:

    class UserStandaard extends User {
    
        /*Setter*/                    
        public function __set($p_sProperty, $p_sValue){
            parent::__set($p_sProperty, $p_sValue);
            // your current code follows
        }
    }
    

    However, this will still not work because now your parent implementation will only recognize the three properties that it knows. There seems to be no direct way to handle this, which means your design is problematic and needs to be modified.

    Possible solution

    Structure __get and __set to be more pluggable (example pending). By this I mean that the set of properties they recognize should not be hardcoded because this makes it very difficult to extend them. Ideally, you would want a different getter and setter for each property:

    class User {
        public function getName() { ... }
        public function setName($name) { ... }
    }
    

    This would let you keep the code nice, clean and separated. You can then wire up __get and __set to forward to these methods:

    public function __get($name)
    {
        $getter = 'get'.ucfirst($name);
        if (!method_exists($this, $getter)) {
            die("This object does not have a $name property or a $getter method.");
        }
    
        return $this->$getter();
    }
    
    public function __set($name, $value)
    {
        $setter = 'set'.ucfirst($name);
        if (!method_exists($this, $setter)) {
            die("This object does not have a $name property or a $setter method.");
        }
    
        $this->$setter($value);
    }
    

    This way you can break down each property into a getter and setter method as above, and also there is absolutely no need to override __get and __set in your derived classes. Simply declare additional getters and setters for the properties you are interested in and the base class __get/__set will correctly forward to these methods.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I have an autohotkey script which looks up a word in a bilingual dictionary
I have been unable to fix a problem with Java Unicode and encoding. The
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but

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.