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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T08:40:16+00:00 2026-06-04T08:40:16+00:00

I am working on a PHP project and I need some help with the

  • 0

I am working on a PHP project and I need some help with the basics. I have a Vehicle class. Within the vehicle class there is a variable called fuel_capacity. As you would expect the fuel capacity changes from vehicle to vehicle. So for example the fuel_capacity changes between a car and a truck. I have a Car class and a Truck class that extend the vehicle class.

On construction of the car I want to assign $fuel_capacity a value. When I do this I can not access $fuel_capacity from the functions within vehicle unless I declare $fuel_capacity public. The problem is if I declare $fuel_capacity public in the Car class then I am worried it will conflict with the $fuel_capacity for the Truck class. Is this correct? If so how do I share the $fuel_capacity with the parent?

Also I want to use a constructor class in my Car class then this overrides my Vehicle class constructor. Is there anyway that I can call for example assign a value such as $fuel_capacity within the Car class and then use that value within the constructor of the Vehicle class?

Finally is there a way to setup an interface for a class that extends from another class?

I have looked at many pages including What's the difference between using normal functions and class methods in PHP?, Understanding inheritance in php, Understanding PHP Inheritance, and What's the difference between using normal functions and class methods in PHP?. These have all been great introduction but they don’t address these questions. 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-04T08:40:17+00:00Added an answer on June 4, 2026 at 8:40 am

    You need to really understand how OOP works and how it is implemented in PHP. http://php.net/oop should be able to help.

    Here’s one way to do it. You can of course set the fuel capacity in constructor. I recommend Vehicle to be an abstract class since you don’t want someone to create a Vehicle object.

    If you inherit of course you can access the variable from Car/Trunk classes since its declared protected.

    abstract class Vehicle {
        public function setFuelCapacity($capacity) {
            $this->fuel_capacity = $capacity;
        }
    
        protected $fuel_capacity;
    }
    
    class Car extends Vehicle {
    }
    
    class Truck extends Vehicle {
    }
    

    Hope that helps.

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

Sidebar

Related Questions

I'm working on a PHP project and I need some help. I need to
I am Working in PHP MySql project, I Have A Page Called Live Information,
In a PHP project I'm working on we need to create some DAL extensions
I'm working on a little project, basically I have some text on my PHP/HTML
Need help to solve some resolution issues on a project i'm currently working on.
I am working on a PHP project. There, I often use following syntax to
Ok, I'm working on a php college project, and I have to let students
My team working on a php/MySQL website for a school project. I have a
I'm working on a project(PHP Based) in which I need to compute SHA1, I'm
I'm currently working on a project about social networking, where I have some problems

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.