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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T08:37:41+00:00 2026-05-27T08:37:41+00:00

I have these classes: class User{ private $user_ID; private $first_name; private $surname; … private

  • 0

I have these classes:

class User{
    private $user_ID;
    private $first_name;
    private $surname;
    ...

    private $website;
    private $company;

    function __construct($array){
        $this->user_ID            = $array["userId"];
        $this->first_name         = $array["first"];
        $this->surname            = $array["last"];
        $this->telephone          = $array["tele"];
        ...
    }
    public function addWebsite($array){
        $this->website = $array;
    }
    public function addCompany($array){
        $this->company = $array;
    }
    public function getData(){
        $array = array();
        foreach($this as $var => $value) {
            $array[$var] = $value;
        }
        return $array;
    }
}

class Website{
    private $webId;
    private $url;
    private $description;
    ...

    function __contruct($array){
        $this->webId           = $array["webId"];
        $this->url             = $array["url"];
        $this->description     = $array["desc"];
        ...
    }
}

the getData() method in User is exactly the same for the Website class.

so how can i get the website class to implement this method? But ONLY the getData() method

  • 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-27T08:37:42+00:00Added an answer on May 27, 2026 at 8:37 am

    While inheritance forms an behaves-as relationship, this is not a situation for Inheritance. Your Website is not related to the User in any way, so there shouldn’t be a relationship between them.

    Having base classes like suggested elsewhere here will quickly lead to monolithic architecture and god objects. Those in turn lead to less maintainability, high coupling, fragile code and hampers reuse. Likewise, making everything public or resorting to similar means that defeat information hiding and widen the public API lead to similar problems and you will want to avoid them.

    What you are looking for is Traits, but these are only supported as of PHP 5.4. The easiest approach is really just to duplicate that method in both classes. Keep in mind that you usually want to avoid code duplication, but in this case its the lesser evil over the other suggested alternatives.

    A viable alternative would be to use an Introspection Service that uses Reflection to fetch the data from the object into an array. Although in general, you should put methods on the objects having the data the methods operate on.

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

Sidebar

Related Questions

I have these classes: class Base { ... private: std::vector<X> v; }; class Derived
I have these two classes public class Person { } public class Company {
I have in my javascript these 2 functions classes: // product class function Product()
Say I have these classes: public class Loan { @Id private Long id; @ManyToOne
Hi i have these classes: class Core < ActiveRecord::Base belongs_to :resource, :polymorphic => true
Concerning data binding I have these classes: public class Foo : List<Bar> { public
Let's say we have these two classes: public class Base { public static int
I have these classes. class Author{ Person person } class Person{ String lastName String
I have these classes in C# (.NET Framework 3.5) described below: public class Base
I have two ActiveRecord classes class Product : ActiveRecordBase<Product> { private IList<Usage> _usages =

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.