PHP | Common terminology in OOP Last Updated : 03 Feb, 2020 Comments Improve Suggest changes Like Article Like Report Object Oriented Programming is a very important concept for programmers to solve real-life problems. This article explaining the concept of object-oriented programming and its features. Object: Everything in this world is surrounded with objects. Table, chair, monitor, cellphone everything is an object. There are two things in an object to remember to solve real-life problems. One is attribute and the other one is behavior. If we talk about monitor so...model number, screen size all these are attributes. On the other hand, the features like volume up or volume down are behavior for the monitor. In programming, variables are attributes and functions are behavior. Example: php <?php // Class definition class TV { // Member variables public $model = 'xyz'; public $volume = 1; // Member Functions function volumeUp() { $this->volume++; } function volumeDown() { $this->volume--; } } // Create new object $tv_one = new TV; // Calling member function $tv_one->volumeUp(); echo $tv_one->volume; ?> Output: 2 In the above example, creating an object $tv_one of TV class and implementation of function showing the behavior of the object. Initially $tv_one->volume was 1. After calling its function volume has been increased and displaying the updated result. $this refers to the particular or current object. You can create multiple objects and show it's behavior. Implementing this code will have the benefit of code re-usability and code will be easy to manage in the future. Constructor function: Constructor function is an special function for which do not need to call the function like earlier we were doing after creating an object. Example: php <?php // 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 new object $tv = new TV('xyz', 2); echo $tv->model; ?> Output: xyz In the above example, assigned the value of model and volume when we are creating the object. This is the benefit of using a constructor function. You don't need to change the model number for each and every project you will be making. All need to do is to include this file and when you need to create an object of TV and assign its value you can do that instantly while creating its object. Inheritance: In a simple word, inheritance means inheriting the properties or functionalities from one class to another class. Model and volume will find in all kinds of television but suppose you need to add additional feature timer. The timer can not be included in all the television. So it will use inheritance for that television which includes timer property. Use 'extends' keyword to implement inheritance. Example: php <?php // 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 new object $tv = new TvWithTimer('xyz', 2); echo $tv->model; ?> Output: xyz Here, TVWithTimer class is a child class for TV class, whereas TV class is a parent class. The TVWithTimer class inherited all the variables and functions from its parent class so all the variables and functions which are in TV class will be available in TvWithTimer. So create multiple classes and inherit the property from TV class. Another important thing is Method Overriding. If defining the constructor function in child class after inheriting all the properties from parent class then the priorities will be given to child class constructor instead of a parent class. php class plazmaTv extends TV { function __construct { } } $plazma = new plazmaTv; echo $plazma->model; Here, no output will display and the screen will be blank because the child class constructor is being called and it has overridden the parent class constructor. The same logic also applies for variables and you can override the variables in a child class as well. Encapsulation: Encapsulation is all about wrapping the data and method in a single unit. Object-Oriented programming sets the access level for member functions or variables. It means from where it needs to be accessed. There are three kinds of access levels or access specifiers. Public: Public functions or variables can only be accessed from anywhere. In all the above examples, we are able to access the public functions and variables outside the class and everyone can call it. Private: Private functions or variables can only be accessed by the class who created it. Example: php <?php // 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 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 display after executing the above code. It means the function or variable not accessible outside the class. Protected: Protected functions or variables can only be used by the class who created and it's child class means all the classes which inherit the property of parent class having protected method or variable. Example: php <?php // 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 new object $tv = new plazma('xyz', 1); echo $tv->getModel(); ?> Output: xyz Abstract Classes: Like the keyword is saying, Abstract means not completed. Abstract classes are those classes where functions are declared but not implemented. Example: php <?php // Abstract class abstract class ABC { public abstract function xyz(); } class def extends ABC { public function xyz() { echo "Welcome to GeeksforGeeks"; } } // Create new object $obj = new def; echo $obj->xyz(); ?> Output: Welcome to GeeksforGeeks Here, we created an abstract class that has one abstract method but we haven't implemented the method in ABC base class. All the classes which will extend ABC class will have the implementation for function XYZ. Here you can not make the object for ABC class. ABC class works as a base class for all it's child classes and the implementation or object creation will be done for child classes. Declaring a class abstract class is enforcement that you don't want to create a direct object for that class. Example: php <?php 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() { return $this->hourlyRate * $this->totalHours; } } $fulltime = new FullTimeEmployee('fulltime', 'Employee'); $contract = new ContractEmployee('Contract', 'Employee'); echo $fulltime->getFullName(); echo $contract->getFullName(); echo $fulltime->getMonthlySalary(); echo $contract->getMonthlySalary(); ?> Output: fulltime EmployeeContract Employee00 Interfaces: There is a limitation for inheritance in PHP that is you can not extend multiple classes. Through Interface you can achieve multiple inheritance property. Interfaces also worked like abstract classes. The 'implements' keyword is used for interfaces. There is a difference between abstract class and interface class. In abstract class define variables but in the interface, the class can not define variables. It can also not create constructor function in interfaces. It can not declare a private or protected function. Example: php <?php interface a { public function abc(); } interface b { public function xyz(); } class c implements a, b { public function abc() { echo 'GeeksforGeeks '; } public function xyz() { echo 'A computer science portal'; } } // Create an object $obj = new c; $obj->abc(); $obj->xyz(); ?> Output: GeeksforGeeks A computer science portal Static Members: Static variables or static functions are directly related with class. It can access static variables or functions directly with the class names. Use the 'static' keyword to declare a static function or static variable. Example: php <?php // Class definition class abc { public static $data = 'GeeksforGeeks '; public static function xyz() { echo "A computer science portal"; } } echo abc::$data; abc::xyz(); ?> Output: GeeksforGeeks A computer science portal Example: This example count how many objects we have created for a class. php <?php class abc { public static $objectCount = 0; public static function getCount() { return self::$objectCount; } public function __construct() { self::$objectCount++; } } $a = new abc(); $b = new abc(); $c = new abc(); $d = new abc(); echo abc::getCount(); ?> Output: 4 Note: If created object instead of static using static function or variable every time the output will be 1. So static variable or keyword is related to class not object. Late Static Binding: In PHP 5.3 a concept of late static binding was introduced. Example: php <?php class DB { protected static $table = ' basetable'; public function select() { echo 'SELECT * FROM' .self::$table; } public function insert() { echo 'INSERT INTO' .self::$table; } } class abc extends DB { protected static $table = 'abc'; } $abc = new abc; $abc->select(); ?> Output: SELECT * FROM basetable Here, class DB extends class abc, but when running this code during compilation time it's taking the table name from abc class, not DB. During the compilation time, the PHP interpreter immediately assigns the value from the parent class. In order to get the variable from the child, the class interpreter needs to assign the value during runtime instead of compilation time which is happening in the above example. echo 'SELECT * FROM' .self::$table; or echo 'INSERT INTO' .self::$table; Here when the interpreter will get the request it won't assign the value during compilation time. It will be in pending for runtime. It will assign the value during runtime time and the output will be SELECT * FROM abc Polymorphism: Polymorphism means many types or many forms. It means suppose you have created an interface also classes that will implement this interface class. These classes will have different functionality and they all will share this common interface. Create an interface with LoggerInterface name in LoggerInterface.php file. php <?php interface LoggerInterface { public function log($message); } ?> Now creating a file EmailLogger.php which will implement this interface. php <?php class EmailLogger implements LoggerInterface { public function log($message) { echo "Logging message to email: $message"; } } ?> Create another file FileLogger.php and implement the interface class. php <?php class FileLogger implements LoggerInterface { public function log($message) { echo "Logging message to file: $message"; } } ?> Another file is DBLogger.php php <?php class DBLogger implements LoggerInterface { public function log($message) { echo "logging message to DB: $message"; } } ?> Now, create a UserProfile.php file where call all these files by only defining interface name using type hinting. Do not need to define the class name for calling each and every class created in a file. php <?php class UserProfile { private $logger; public function createUser() { echo "creating user"; $this->logger->log("User Created"); } public function updateUser() { echo "updating user"; $this->logger->log("User Updated"); } public function deleteUser() { echo "deleting user"; $this->logger->log("Deleting User"); } public function __construct(LoggerInterface $logger) { $this->logger = $logger; } } ?> Now, create a file index.php php <?php function __autoload($class) { // classes is a directory where all // the above files are placed. include_once "classes/$class.php"; } function getLogger($type) { switch($type) { case 'email': return new EmailLogger(); break; case 'database': return new DBLogger(); break; case 'file': return new FileLogger(); break; } } $logger = getLogger('database'); $profile = new UserProfile($logger); $profile->createUser(); ?> Output: Creating User. Logging message to DB: User Created So, from this example create multiple files and define their functionality. All need to implement the interface class and just give the interface name in type hinting name as I have defined in UserProfile.php file. Traits: Traits was introduced in PHP5.4. It is the mechanism for code reuse in single inheritance language. The 'trait' keyword is used to define it. Example: php <?php class abc { public function test() { echo "test from class ABC"; } } trait test { public function test2() { echo "test2 method of test trait"; } } class one extends abc { use test; } class two extends abc { use test; } class three extends abc { } class four extends abc { } class five extends abc { } $obj = new one; $obj->test2(); ?> Output: test2 method of test trait Here, taking a class abc and its functionality in all the child classes. So accessing the properties of abc class in all the child classes. Now consider a scenario where we need another function to be accessed only in class one and two but not in the rest of the classes. If you will define that function in the same class abc it will be available in all the classes which are extending class abc. So the solution is we can define that method in trait. Trait is also similar to the classes. Here, define test2 function in trait test and use this trait by using 'use' keyword in child class. So we have used this trait only in class one and two because we need to access this method in class one and two. Method Overriding in Traits: If defining the same method implemented in class and in trait also then preference will be given to the trait class method implementation. It means a trait class method will override the parent class method. Example: php <?php class Base { public function abc() { echo "ABC method from base class"; } } Trait Test { public function abc() { echo "ABC method from test trait"; } } class child extends Base { use Test; } $obj = new Child; $obj->abc(); ?> Output: ABC method from test trait So, the order of preference is child class method > trait class method > parent class method. Access level in traits: You can change the access level of traits in child class. Example: php <?php trait abc { public function xyz() { echo "xyz method from trait abc"; } } class test { use abc { abc::xyz as public abcXyz; } } $obj = new test; $obj->abcXyz(); ?> Output: xyz method from trait abc Namespaces: In PHP, class can not be redeclare. It means a class can not be declared more than once. It will display an error message. To solve this problem the concept of a namespace is used. Example: php <?php namespace def { class xyz { public function __construct() { echo "XYZ from DEF namespace"; } } } namespace { class xyz { public function __construct() { echo "XYZ from Global namespace"; } } $obj = new def/xyz(); } ?> Output: XYZ from DEF namespace By default, all the classes are part of the global namespace. The global namespace declared with only keyword 'namespace' (no need to define the name of the namespace). The code can not be declared outside the namespace. Also, namespace should be the first line in your code if you are declaring a class in it. There is alternate way as well to call the function from def class. just replace the code like below when you create an object. use def\xyz as def; $obj = new def(); The output will be the same. You can also make sub-namespace for a namespace. All you just need to do when you declare it is below the line. namespace def\ghi Type Hinting: In PHP, don't need to define the data type of variable you are declaring. It automatically defines the data type when creating a variable, but sometimes when you receive a variable, there you need to define what kind of variable you are receiving. Example: php <?php // Function definition function test(array $arr) { foreach($arr as $k => $v) { echo $k . " " . $v . "\n"; } } // Declare an array $array = ['abc'=>'ABC', 'xyz'=>'XYZ']; // Function call test($array); ?> Output: abc ABC xyz XYZ So, when receiving the variable and defined that it should be an array type. If you will parse string type data $array ='abcdefgh' then you will get a fatal error and the code won't execute. You can also do type hinting for class name and interface. Comment More infoAdvertise with us Next Article PHP Syntax A anuupadhyay Follow Improve Article Tags : Web Technologies PHP PHP Programs Similar Reads PHP Tutorial PHP is a popular, open-source scripting language mainly used in web development. It runs on the server side and generates dynamic content that is displayed on a web application. PHP is easy to embed in HTML, and it allows developers to create interactive web pages and handle tasks like database mana 9 min read BasicsPHP SyntaxPHP, a powerful server-side scripting language used in web development. Itâs simplicity and ease of use makes it an ideal choice for beginners and experienced developers. This article provides an overview of PHP syntax. PHP scripts can be written anywhere in the document within PHP tags along with n 4 min read PHP VariablesA variable in PHP is a container used to store data such as numbers, strings, arrays, or objects. The value stored in a variable can be changed or updated during the execution of the script.All variable names start with a dollar sign ($).Variables can store different data types, like integers, strin 5 min read PHP | FunctionsA function in PHP is a self-contained block of code that performs a specific task. It can accept inputs (parameters), execute a set of statements, and optionally return a value. PHP functions allow code reusability by encapsulating a block of code to perform specific tasks.Functions can accept param 8 min read PHP LoopsIn PHP, Loops are used to repeat a block of code multiple times based on a given condition. PHP provides several types of loops to handle different scenarios, including while loops, for loops, do...while loops, and foreach loops. In this article, we will discuss the different types of loops in PHP, 4 min read ArrayPHP ArraysArrays are one of the most important data structures in PHP. They allow you to store multiple values in a single variable. PHP arrays can hold values of different types, such as strings, numbers, or even other arrays. Understanding how to use arrays in PHP is important for working with data efficien 5 min read PHP Associative ArraysAn associative array in PHP is a special array where each item has a name or label instead of just a number. Usually, arrays use numbers to find things. For example, the first item is at position 0, the second is 1, and so on. But in an associative array, we use words or names to find things. These 4 min read Multidimensional arrays in PHPMulti-dimensional arrays in PHP are arrays that store other arrays as their elements. Each dimension adds complexity, requiring multiple indices to access elements. Common forms include two-dimensional arrays (like tables) and three-dimensional arrays, useful for organizing complex, structured data. 5 min read Sorting Arrays in PHPSorting arrays is one of the most common operation in programming, and PHP provides a several functions to handle array sorting. Sorting arrays in PHP can be done by values or keys, in ascending or descending order. PHP also allows you to create custom sorting functions.Table of ContentSort Array in 4 min read OOPs & InterfacesPHP ClassesA class defines the structure of an object. It contains properties (variables) and methods (functions). These properties and methods define the behavior and characteristics of an object created from the class.Syntax:<?phpclass Camera { // code goes here...}?>Now, let us understand with the hel 2 min read PHP | Constructors and DestructorsIn PHP, constructors and destructors are special methods that are used in object-oriented programming (OOP). They help initialize objects when they are created and clean up resources when the object is no longer needed. These methods are part of the class lifecycle.In this article, we will discuss w 5 min read PHP Access ModifiersIn object-oriented programming, access specifiers are also known as access modifiers. These specifiers control how and where the properties or methods of a class can be accessed, either from inside the class, from a subclass, or from outside the class. PHP supports three primary access specifiers: p 4 min read Multiple Inheritance in PHPMultiple Inheritance is the property of the Object Oriented Programming languages in which child class or sub class can inherit the properties of the multiple parent classes or super classes. PHP doesn't support multiple inheritance but by using Interfaces in PHP or using Traits in PHP instead of cl 4 min read MySQL DatabasePHP | MySQL Database IntroductionWhat is MySQL? MySQL is an open-source relational database management system (RDBMS). It is the most popular database system used with PHP. MySQL is developed, distributed, and supported by Oracle Corporation. The data in a MySQL database are stored in tables which consists of columns and rows.MySQL 4 min read PHP Database connectionThe collection of related data is called a database. XAMPP stands for cross-platform, Apache, MySQL, PHP, and Perl. It is among the simple light-weight local servers for website development. Requirements: XAMPP web server procedure: Start XAMPP server by starting Apache and MySQL. Write PHP script f 2 min read PHP | MySQL ( Creating Database )What is a database? Database is a collection of inter-related data which helps in efficient retrieval, insertion and deletion of data from database and organizes the data in the form of tables, views, schemas, reports etc. For Example, university database organizes the data about students, faculty, 3 min read PHP | MySQL ( Creating Table )What is a table? In relational databases, and flat file databases, a table is a set of data elements using a model of vertical columns and horizontal rows, the cell being the unit where a row and column intersect. A table has a specified number of columns, but can have any number of rows. Creating a 3 min read PHP AdvancePHP SuperglobalsPHP superglobals are predefined variables that are globally available in all scopes. They are used to handle different types of data, such as input data, server data, session data, and more. These superglobal arrays allow developers to easily work with these global data structures without the need t 6 min read PHP | Regular ExpressionsRegular expressions commonly known as a regex (regexes) are a sequence of characters describing a special search pattern in the form of text string. They are basically used in programming world algorithms for matching some loosely defined patterns to achieve some relevant tasks. Some times regexes a 12 min read PHP Form HandlingForm handling is the process of collecting and processing information that users submit through HTML forms. In PHP, we use special tools called $_POST and $_GET to gather the data from the form. Which tool to use depends on how the form sends the dataâeither through the POST method (more secure, hid 4 min read PHP File HandlingIn PHP, File handling is the process of interacting with files on the server, such as reading files, writing to a file, creating new files, or deleting existing ones. File handling is essential for applications that require the storage and retrieval of data, such as logging systems, user-generated c 4 min read PHP | Uploading FileHave you ever wondered how websites build their system of file uploading in PHP? Here we will come to know about the file uploading process. A question which you can come up with - 'Are we able to upload any kind of file with this system?'. The answer is yes, we can upload files with different types 3 min read PHP CookiesA cookie is a small text file that is stored in the user's browser. Cookies are used to store information that can be retrieved later, making them ideal for scenarios where you need to remember user preferences, such as:User login status (keeping users logged in between sessions)Language preferences 9 min read PHP | SessionsA session in PHP is a mechanism that allows data to be stored and accessed across multiple pages on a website. When a user visits a website, PHP creates a unique session ID for that user. This session ID is then stored as a cookie in the user's browser (by default) or passed via the URL. The session 7 min read Like