Constants in classes
📖 Scenario: Imagine you are creating a simple program to represent different types of vehicles. Each vehicle type has a fixed number of wheels that never changes. You want to store these fixed numbers as constants inside a class to keep your code organized and clear.
🎯 Goal: You will create a PHP class called Vehicle with constants for different vehicle types and their fixed number of wheels. Then, you will print out the number of wheels for a specific vehicle type using the class constant.
📋 What You'll Learn
Create a class named
Vehicle.Inside the class, define constants
CAR_WHEELS with value 4 and MOTORCYCLE_WHEELS with value 2.Outside the class, print the value of the constant
CAR_WHEELS using the class name.💡 Why This Matters
🌍 Real World
Constants in classes help keep fixed values organized and easy to maintain, like fixed settings or limits in a program.
💼 Career
Understanding class constants is important for writing clean, maintainable code in PHP, which is widely used in web development jobs.
Progress0 / 4 steps