Skip to main content

Oop features in PHP 7

 OOP (Object-Oriented Programming) is a programming paradigm that utilizes objects to represent and manipulate data. PHP supports OOP features, which include:

  1. Classes: In PHP, a class is a blueprint for creating objects. It defines properties and methods that an object can have.

  2. Objects: An object is an instance of a class. It contains properties and methods that define its behavior.

  3. Encapsulation: This is the ability to encapsulate data and functionality within a class. This means that data is hidden from the outside world, and only methods that belong to the class can access it.

  4. Inheritance: Inheritance allows a class to inherit properties and methods from another class. This promotes code reuse and simplifies code maintenance.

  5. Polymorphism: This is the ability of an object to take on different forms. In PHP, this is achieved through method overloading and overriding.

  6. Abstraction: This is the ability to define abstract classes and methods that can be implemented by subclasses. It allows for the creation of generic classes and methods that can be reused in different contexts.

Here are a few examples of classes and objects in PHP(version < 8):

Car Class:

<?php

class Car {
    public $make;
    public $model;
    public $year;
    
    public function __construct($make, $model, $year) {
        $this->make = $make;
        $this->model = $model;
        $this->year = $year;
    }
    
    public function getMake() {
        return $this->make;
    }
    
    public function getModel() {
        return $this->model;
    }
    
    public function getYear() {
        return $this->year;
    }
}

// Creating an object of Car class
$car = new Car("Toyota", "Corolla", "2015");
echo $car->getMake(); // Outputs: Toyota


?>

Explaination:

This is an example of a Car class in PHP that has three properties - make, model, and year - and three methods - getMake, getModel, and getYear.

The constructor method is used to initialize the object properties when the object is created. In this case, the constructor takes three arguments - make, model, and year - and sets the corresponding properties of the object.

The getMake, getModel, and getYear methods are used to retrieve the values of the object properties. They are defined as public, so they can be accessed from outside the class.

In the last section of the code, an object of the Car class is created and initialized with the values "Toyota", "Corolla", and "2015". The getMake method is then called on the object, which returns the value of the make property ("Toyota"). The output of the code would be "Toyota".

User Class:


<?php

class User {
    public $username;
    public $email;
    
    public function __construct($username, $email) {
        $this->username = $username;
        $this->email = $email;
    }
    
    public function getUsername() {
        return $this->username;
    }
    
    public function getEmail() {
        return $this->email;
    }
}

// Creating an object of User class
$user = new User("johndoe", "johndoe@example.com");
echo $user->getUsername(); // Outputs: johndoe


?>
Explaination:

This is an example of a User class in PHP that has two properties - username and email - and two methods - getUsername and getEmail.

The constructor method is used to initialize the object properties when the object is created. In this case, the constructor takes two arguments - username and email - and sets the corresponding properties of the object.

The getUsername and getEmail methods are used to retrieve the values of the object properties. They are defined as public, so they can be accessed from outside the class.

In the last section of the code, an object of the User class is created and initialized with the values "johndoe" and johndoe@example.com. The getUsername method is then called on the object, which returns the value of the username property ("johndoe"). The output of the code would be "johndoe".

This is a simple example of how objects can be used to store and retrieve data, such as user information in this case. The methods allow for controlled access to the object's properties, preventing direct modification and ensuring the data is valid.

Book Class:

<?php

class Book {
    public $title;
    public $author;
    public $price;
    
    public function __construct($title, $author, $price) {
        $this->title = $title;
        $this->author = $author;
        $this->price = $price;
    }
    
    public function getTitle() {
        return $this->title;
    }
    
    public function getAuthor() {
        return $this->author;
    }
    
    public function getPrice() {
        return $this->price;
    }
}

// Creating an object of Book class
$book = new Book("The Great Gatsby", "F. Scott Fitzgerald", "19.99");
echo $book->getTitle(); // Outputs: The Great Gatsby


?>

Explaination:

This is an example of a Book class in PHP that has three properties - title, author, and price - and three methods - getTitle, getAuthor, and getPrice.

The constructor method is used to initialize the object properties when the object is created. In this case, the constructor takes three arguments - title, author, and price - and sets the corresponding properties of the object.

The getTitle, getAuthor, and getPrice methods are used to retrieve the values of the object properties. They are defined as public, so they can be accessed from outside the class.

In the last section of the code, an object of the Book class is created and initialized with the values "The Great Gatsby", "F. Scott Fitzgerald", and "19.99". The getTitle method is then called on the object, which returns the value of the title property ("The Great Gatsby"). The output of the code would be "The Great Gatsby".

This is a simple example of how objects can be used to store and retrieve data, such as book information in this case. The methods allow for controlled access to the object's properties, preventing direct modification and ensuring the data is valid.

Comments

Popular Posts

List of latest and most asked PHP practical interviews questions & answers

Core PHP Practical Interview Questions In this blog post I am sharing a list of some most asked PHP interview questions & answers. These are very useful and helpful for the freshers and experienced developer too. I have taken these questions from different sources and listed here at one place. Ques. How to reverse a string without using any builtin function? Ans: <?php $str = 'My name is Diwakar Kumar'; $len = 0; while(isset($str[$len]) != '') $len++; for($i = $len ; $i >= 0 ; $i--) { echo @$str[$i]; } Ques: Write a function to check if a given string is a palindrome or not. Ans: 1st Method: <?php function isPalindrome($str) { $str = strtolower(preg_replace('/[^a-zA-Z0-9]/', '', $str)); // Convert to lowercase and remove non-alphanumeric characters $reverse = strrev($str); // Reverse the string return $str === $reverse; // Compare original and reversed string } 2nd Method: <?php funct

Bootstrap Responsive Media Queries CSS Tips

AS a developer I faces lots of problem while making website responsive to make website visible correctly on all devices like mobile, tablet and desktop. So, today I am sharing some Bootstrap responsive CSS styles and Media Queries  tips with you. But make sure that you are not repeating the same media queries for the same screen size. Otherwise it will override your previous CSS style rules.    The Grid Sizes .col-xs-$ => Extra Small (Phones Less than 768px) .col-sm-$ => Small Devices (Tablets 768px and Up) .col-md-$ => Medium Devices (Desktops 992px and Up) .col-lg-$  => Large Devices (Large Desktops 1200px and Up) Here is the Responsive CSS Style for all Screen Sizes Read more: https://scotch.io/tutorials/default-sizes-for-twitter-bootstraps-media-queries

How to take user input from terminal(stdin) in Rust?

In Rust, you can use the std::io module from the standard library to read input from the user. Here's an example that demonstrates how to get input from the user: use std::io; fn main() { // Create a new instance of `std::io::stdin` for reading user input let mut input = String::new(); // Prompt the user for input println!("Enter your name:"); // Read input from the user io::stdin() .read_line(&mut input) .expect("Failed to read line"); // Trim any trailing whitespace or newlines from the input let name = input.trim(); // Display the user's input println!("Hello, {}!", name); } In this example, we create a mutable String variable named input to store the user's input. We then use the std::io::stdin() function to obtain a handle to the standard input stream. Next, we call the read_line() method on the input stream, passing a mutable reference to the input variable. The r