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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T16:02:19+00:00 2026-06-09T16:02:19+00:00

I am new to PHP and just get into OOP. I have few generic

  • 0

I am new to PHP and just get into OOP. I have few generic methods used to set and get properties. I use them quite often in almost all the classes, i put those methods in a class and extends other classes from it. Now i can access the methods from child class but dont know how set and get attributes of child class through them … parent class base.php

class Base
{
    public function __construct()
    {
    }

    function __set($propName, $propValue)
    {
        $this->$propName = $propValue;
    }

    function setProperties(array $data)
    {
        foreach($data as $propName => $propValue)
            $this->$propName = $propValue;
    }

    function __get($propName)
    {
        return (isset($this->$propName))? $this->$propName : "Invalid property!";
    }

    function getProperties(array $properties)
    {
        foreach($properties as $propName)
            $propVals[$propName] = $this->$propName;
        return $propVals;
    }
}

child class categories.php

class categories extends Base
{

    private $id;
    private $pid;
    private $title;
    private $status;

    public function __construct()
    {
        parent::__construct();  
    }
}

and i called it like

$objCat = new categories();

$objCat->setProperties(array('id'=>'10', 'pid'=>'6'));

print_r( $objCat->getProperties(array('id', 'pid')));

Need little guidance here. If its the right way? or at least is it possible to do it like this? if so how to accomplish this …
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-09T16:02:21+00:00Added an answer on June 9, 2026 at 4:02 pm

    Extending a class is something you only want to do when you can say class categories is a class Base. Something like that sort of utility class you have their is almost always the wrong way to go. PHP also has introduced something called traits for copy/paste code. However my personal preference is that it is something you will never want to use, because it tightly couples the traits to your class, which is something you want to avoid.

    See for more information the Liskov Substitution principle in SOLID programming.

    If it was up to me I would avoid those magic getters / setters either way and just add your own getters / setters methods to the class.

    The mistake about some base class isn’t something only you are doing (hell even I have done it in the past). Think about some class Database and a class Article. Because the Article class needs access to the database many people let the class extend the Database class. This isn’t correct because an article isn’t an database. Instead an instance of the database class should be injected into the article class by using dependency injection. The instance should either be injected into the class constructor (if all or many methods need access to it) or just the methods that need it. So when extending a class you have to be able to say class B is a class A.

    Some other notes about your code:

    • Always make your class names PascalCase. This is not really required to make your code work, but it follows a naming convention often used.
    • And my personal preference a bit: please always add curly braces to your foreach statements. It is more clear what is happening when other people are reading your code.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to PHP development and I just used Zend yesterday. One problem I'm
I'm starting a new webapp project in Python to get into the Agile mind-set
I am just getting into OOP and framework design. I have started by using
Tech used: MySQL 5.1 and PHP 5.3 I am just designing a new database
I am new to PHP, I practised PHP setcookie() just now and failed. http://localhost/test/
I am pretty new to PHP and MySQL and I just can't figure this
I've just started messing around with php recently and I was testing my new
I am trying to use a PHP form (new.php) to update 2 MySQL tables
New to PHP and MySQL, have heard amazing things about this website from Leo
I'm making a new php page and I get the error Fatal error: Call

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.