Skip to main content

Learn Object Oriented Programming PHP fundamentals bootcamp | Lecture-13

Extending Object-Oriented Programming in PHP: A Fundamentals Bootcamp

If you're looking to master object-oriented programming in PHP, Udemy's PHP Fundamentals Bootcamp is the course for you. In lecture 13, you'll learn how to use the "extends" keyword to extend classes and reuse code efficiently.

What is "extends" in Object-Oriented Programming?

The "extends" keyword is a fundamental feature of object-oriented programming (OOP). It allows you to create a new class based on an existing class (the parent or superclass), inheriting all of its properties and methods. You can then add your own unique properties and methods to the new class (the child or subclass), without having to rewrite all of the code that already exists in the parent class.

Benefits of "extends"

Using "extends" in OOP has several advantages, including:
  • Code reusability: By extending an existing class, you can reuse code and avoid duplicating it in your new class. This leads to more efficient and less error-prone coding.
  • Simplicity: Extending classes allows you to keep your code simple and organized. You can create a hierarchy of classes that builds on each other, with each class having its own unique properties and methods.
  • Flexibility: Extending classes gives you the flexibility to make changes to your code without having to rewrite everything. You can add or remove methods, and properties, and even change the behavior of the existing methods in your subclass.

How to Use "extends" in PHP

Let's take a closer look at how to use "extends" in PHP. In lecture 13 of the PHP Fundamentals Bootcamp, the instructor provides an example of how to use "extends" to create a new class based on an existing class.

The example uses a car class as the parent class, which has properties such as car make, gas units, and engine units. The instructor then creates a new class called Hummer, which extends the car class.

In the Hummer class, the instructor changes the gas units and engine units to different values than the original car class, but still uses the car make property from the parent class. This demonstrates how you can reuse code from the parent class, while making changes to specific properties or methods in the child class.

Planning Your Object-Oriented Structure

While using "extends" can make your coding more efficient, it's important to plan your object-oriented structure carefully. In lecture 13, the instructor emphasizes the importance of planning out which parts of the class you want to extend and reuse, and which parts you need to write new code for.

To make your code as efficient as possible, you should also consider which methods and properties will be used outside of the class, and make sure they are set to public. Methods and properties used within the class itself can be set to private, while those used in the extended class can be set to protected.

In conclusion, using "extends" in PHP is a powerful way to reuse code and make your coding more efficient. By extending an existing class, you can create a hierarchy of classes that builds on each other and adds unique functionality to your code.




  • The lesson focuses on the concept of "extend" in object-oriented programming in PHP.
  • Extending allows for the reuse of classes to minimize code and make adjustments to create entirely different classes while keeping basic functionality.
  • The example given involves creating a Hummer class that extends a Car class and making adjustments to its gas tank size.
  • Access modifiers such as public, protected, and private are discussed, and their use depends on where the methods are being accessed from.
  • Planning is essential when creating an object-oriented structure to efficiently use code and minimize redundancies.

Comments

Popular posts from this blog

Bangladesh's e-commerce revolution: How online sales are transforming the ready-made garment industry

 Table of Contents Introduction The growth of e-commerce in Bangladesh The impact on the ready-made garment industry Challenges and opportunities Conclusion Introduction Bangladesh is known for its booming ready-made garment (RMG) industry, which accounts for around 80% of the country's total exports. However, in recent years, the country has also seen a rapid growth in e-commerce, with online sales projected to reach $1 billion by 2023. This article will explore how the rise of e-commerce in Bangladesh is transforming the RMG industry and the challenges and opportunities that come with it. The growth of e-commerce in Bangladesh The e-commerce market in Bangladesh has been growing at an astonishing rate, with a compound annual growth rate (CAGR) of more than 60% over the past few years. This can be attributed to several factors, including an increasing number of internet users, a growing middle class, and the ease of access to online shopping platforms. In addition, the government ...

Learn Object Oriented Programming PHP fundamentals bootcamp | Lecture-9

Learn Object-Oriented Programming with PHP: Changing Property Values Object Oriented Programming (OOP) is a fundamental aspect of modern software development. PHP is one of the most popular programming languages used for web development, and understanding how to use OOP in PHP is essential for creating robust and efficient web applications. In this episode of the Udemy PHP Fundamentals Bootcamp, the instructor teaches how to change property values in OOP using PHP. What is covered in the episode? The instructor begins by adding new properties and methods to the original class of the car blueprint. They demonstrate how to use the use gas method to take in a value of the amount of gas used and calculate the remaining gas in the engine. By continuously accessing the use gas method, the amount of gas in the engine can be updated. Why is this important? Understanding how to change property values in OOP using PHP is essential for building efficient and scalable web applications. By using OO...

Learn Object Oriented Programming PHP fundamentals bootcamp | Lecture-11

How to Benefit from a Conversation on Overcoming Procrastination Procrastination is a problem that affects millions of people worldwide. It can cause stress, anxiety, and decreased productivity. If you're struggling with procrastination, it's essential to seek help and guidance from experts who can help you overcome it. In this article, we'll discuss how you can benefit from a conversation on overcoming procrastination and why it's essential. What is Procrastination? Procrastination is the act of delaying or putting off tasks or actions that need to be completed. It's a common problem that affects people of all ages and can lead to stress, anxiety, and reduced productivity. How Can a Conversation on Overcoming Procrastination Help You? A conversation on overcoming procrastination can help you in several ways, including: 1. Learning Effective Strategies During a conversation on overcoming procrastination, you can learn effective strategies that can help you overcome ...