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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T19:06:38+00:00 2026-06-05T19:06:38+00:00

I’m having a little difficulty coming up with the OOP design of a small

  • 0

I’m having a little difficulty coming up with the OOP design of a small php app I’m building. I have information for Restaurants in a database, split out into a restaurant table and a locations table. Both tables have a few common columns, such as phone, website, and logo url. Obviously the relation between locations and restaurants is many-to-one.

So here’s the issue: I want to create a Restaurant class that has all the information related to the global restaurant information, such as name, phone, website, logo, etc. Then I want to make a Location class that contains location-specific information such as address, phone, website, logo, etc.

The problem that I’m running into is that I want to be able to instantiate both object types, but also want to have the Location class fall back on the parent data if it’s not present in itself. Normally, you’d be able to write something like this (abbreviated):

class Restaurant {

    protected $phone;

    function __construct($restaurant_id) {
        // Perform db call here and set class attributes
    }

    public function getPhone() {
        return $this->phone;
    }
}

class Location extends Restaurant {

    function __construct($location_id) {
        // Perform db call here and set class attributes

        // $restaurant_id would be loaded from the DB above
        parent::__construct($restaurant_id)
    }
}

$location = new Location(123);
echo $location->getPhone();

$restaurant = new Restaurant(456);
echo $restaurant->getPhone();

But like I said, I want the getPhone() method to first check $this->phone, and if it doesn’t exist, the fall back to the parent. Would something like this be the correct way?

class Restaurant {

    private $phone;

    function __construct($restaurant_id) {
        // Perform db call here and set class attributes
    }

    public getPhone() {
        return $this->phone;
    }
}

class Location extends Restaurant {

    private $phone;

    function __construct($location_id) {
        // Perform db call here and set class attributes

        // $restaurant_id would be loaded from the DB above
        parent::__construct($restaurant_id)
    }

    public function getPhone() {
        if(!empty($this->phone)) {
            return $this->phone;
        }
        return parent::getPhone();
    }
}

$location = new Location(123);
echo $location->getPhone();

I feel like the above code is really hacky, and there’s probably a much better way of accomplishing this. Since the two have common attributes, would it be better for the Location class to not extend Restaurant but instead hold a variable of type Restaurant for the “parent” object? Then in the Location::getPhone() method, it performs a similar if(empty()) check?

  • 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-05T19:06:39+00:00Added an answer on June 5, 2026 at 7:06 pm

    Location should not extend Restaurant because it isn’t a restaurant itself; it’s one of many locations of that restaurant.

    class Location {
        private $restaurant;
        private $phone;
    
        public function getPhone() {
            return $this->phone ?: $restaurant->getPhone();
        }
    }
    

    Now, with so many fields in common between the two classes, you may want to define a common base class that they each extend, such as CompanyInfoHolder that contains website, phone, and logo. In this case, Location would override getPhone exactly as above.

    • 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
We're building an app, our first using Rails 3, and we're having to build
I have a view passing on information from a database: def serve_article(request, id): served_article
link Im having trouble converting the html entites into html characters, (&# 8217;) i
this is what i have right now Drawing an RSS feed into the php,
I have a small JavaScript validation script that validates inputs based on Regex. I
I have an array which has BIG numbers and small numbers in it. I
I have a reasonable size flat file database of text documents mostly saved in
Let's say I'm outputting a post title and in our database, it's Hello Y’all
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example

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.