Change language

PHP Common terminology in OOP

|
Object Oriented Programmingis a very important concept for programmers to solve real-world problems. This article explains the concept of object-oriented programming and its features.
  • Object:Everything in this world is surrounded by objects. Table, chair, monitor, mobile phone - everything is an object. There are two things to remember in order to solve real problems. One - this is an attribute and the other - behavior. In terms of the monitor, so ... model number, screen size - these are all attributes. On the other hand, features like volume up or down - this is the behavior of the monitor. In programming, variables are attributes, and functions - Example :  
    // Class definition class TV { // member variables public $model = ’xyz’ ; public $volume = 1; // Member functions function volumeUp() { $this -> volume ++; } function volumeDown ( ) { $this -> volume--; } }  
    // Create a new object $tv_one = new TV;  
    // Call member function $tv_one -> volumeUp(); echo $tv_one -> volume ;  
    ?>
    Exit:
    2  In the above example, create a $tv_one object of the TV class and implement a function that shows the behavior of the object. Originally $tv_one-> volume was 1. After calling its function, the volume was increased and the updated result is displayed. $this refers to a specific or current object. You can create multiple objects and show their behavior. Implementing this code will have the advantage of being reusable and easy to manage code in the future.
  • Function-constructor: function-constructor - this is a special function that does not need to call the function, as we did earlier after creating the object. Example :  
    // Class definition class TV { // member variables public $model ; public $volume ; // Member functions function volumeUp() { $this -> volume ++; } function volumeDown ( ) { $this -> volume--; } function __ construct ( $m , $v ) { $this -> model = $m ; $this -> volume = $v ; } }  
    // Create a new object $tv = new TV ( ’ xyz’ , 2); echo $tv -> model;  
    ?>
    Exit:
    xyz  The above example assigns a model and volume value when the object is created. This is the advantage of using a constructor function. You don’t need to change the model number for every project you do. All you have to do is include this file, and when you need to create a TV object and assign a value to it, you can do it instantly when you create this object.
  • Inheritance. In short, inheritance means inheriting properties or functionality from one class to another. The model and volume can be found in all kinds of TVs, but suppose you need to add an additional timer function. The timer cannot be enabled on all TVs. So it will use inheritance for TVs that include a timer property. Use the extends keyword to implement inheritance. Example :  
    // Class definition class TV { // member variables public $model ; public $volume ; // Member functions function volumeUp() { $this -> volume ++; } function volumeDown ( ) { $this -> volume--; } function __ construct ( $m , $v ) { $this -> model = $m ; $this -> volume = $v ; } } class TvWithTimer extends TV { public $timer = true; }  
    // Create a new object $tv = new TvWithTimer ( ’xyz’ , 2); echo $tv -> model;  
    ?>
    Exit:
    xyz  Here the TVWithTimer class is a child class of the TV class, while the TV class is the parent class. The TVWithTimer class has inherited all the variables and functions from its parent class, so all the variables and functions that are in the TV class will be available in TvWithTimer. Therefore, create several classes and inherit the property from the TV class. Another important thing - method override . If you define a constructor function in a child class after inheriting all properties from the parent class, then the constructor of the child class will take precedence over the parent class. 

    class plazmaTv extends TV { function __ construct { } } $plazma = new plazmaTv; echo $plazma -> model; Here the output will not be displayed and the screen will be blank, therefore that the constructor of the child class is called and it has overridden the constructor of the parent class. The same logic applies to variables, and you can also override variables in the child class.
  • Encapsulation:Encapsulation - that’s all about packing the data and the method into one block. In object-oriented programming, set the access level for member functions or variables. This means where it needs to be accessed from. There are three kinds of access levels or access specifiers:
  • Public:public functions or variables are only accessible from anywhere. In all of the above examples, we can access public functions and variables outside the class and anyone can call it.
  • Private:private functions or variables can only be accessed by the class who created them. Example :  
    // Class definition class TV { // member variables private $model ; public $volume ; // Member functions function volumeUp() { $this -> volume ++; } function volumeDown ( ) { $this -> volume--; } function __ construct ( $m , $v ) { $this -> model = $m ; $this -> volume = $v ; } }  
    // Create a new object $tv = new TV ( ’ xyz’ , 2); echo $tv -> model;  
    ?>
    Output:
    Cannot access private property TV::$model in /home/65af96133e297a6da6c1ad3d75c9ff46.php:28 
    The error will be displayed after executing the above code. This means that the function or variable is not accessible outside the class.
  • Protected:Protected functions or variables can only be used by the created class, and its child class means all classes, which inherit a property of a parent class that has a protected method or variable. Example :  
    // Class definition class TV { // member variables protected $model ; public $volume ; // Member functions function volumeUp() { $this -> volume ++; } function volumeDown ( ) { $this -> volume--; } function getModel() { return $this -> model; }   function __ construct ( $m , $v ) {

    $this -> model = $m ; $this -> volume = $v ; } } class plazma extends TV { function getModel() { return $this -> model; } }  
    // Create a new object $tv = new plazma ( ’ xyz’ , 1); echo $tv -> getModel();  
    ?>
    Exit:
    xyz     
  • Abstract classes:As the keyword says, abstract means not complete. Abstract classes - these are the classes where functions are declared but not implemented. Example :  
    // Abstruct class abstract class ABC { public abstract function xyz(); } class def extends ABC { public function xyz() { echo "Welcome to GeeksforGeeks" ; } }  
    // Create a new object $obj = new def; echo $obj -> xyz();  
    ?>
    Exit:
    Welcome to GeeksforGeeks 
    Here we have created an abstract class that has one abstract method, but we have not implemented the method in the base ABC class. All classes that extend the ABC class will have an implementation for the xyz function. Here you cannot make an object for class ABC. The ABC class works as a base class for all its child classes, and the implementation or object creation will be done for the child classes. Declaring an abstract class of a class is a prerequisite that you do not want to create a direct object for that class.Example : abstract class BaseEmployee {   protected $firstname ; protected $lastname ; public function getFullName() { return $this -> firstname. "" . $this -> lastname; } public abstract function getMonthlySalary(); public function __ construct ( $f , $l ) { $this -> firstname = $f ; $this -> lastname = $l ; } } class FullTimeEmployee extends BaseEmployee { protected $annualSalary ; public function getMonthlySalary() { return $this -> annualSalary / 12; } } class ContractEmployee extends BaseEmployee { protected $hourlyRate ; protected $totalHours ; public function getMonthlySalary() { code class = "keyword"> extends BaseEmployee { protected $hourlyRate ; protected $totalHours ; public function getMonthlySalary() { code class = "keyword"> extends BaseEmployee { protected $hourlyRate ; protected $totalHours ; public function getMonthlySalary() {                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  return $

    Shop

    Learn programming in R: courses

    $

    Best Python online courses for 2022

    $

    Best laptop for Fortnite

    $

    Best laptop for Excel

    $

    Best laptop for Solidworks

    $

    Best laptop for Roblox

    $

    Best computer for crypto mining

    $

    Best laptop for Sims 4

    $

    Latest questions

    NUMPYNUMPY

    psycopg2: insert multiple rows with one query

    12 answers

    NUMPYNUMPY

    How to convert Nonetype to int or string?

    12 answers

    NUMPYNUMPY

    How to specify multiple return types using type-hints

    12 answers

    NUMPYNUMPY

    Javascript Error: IPython is not defined in JupyterLab

    12 answers


    Wiki

    Python OpenCV | cv2.putText () method

    numpy.arctan2 () in Python

    Python | os.path.realpath () method

    Python OpenCV | cv2.circle () method

    Python OpenCV cv2.cvtColor () method

    Python - Move item to the end of the list

    time.perf_counter () function in Python

    Check if one list is a subset of another in Python

    Python os.path.join () method