0%
Loading ...

PHP OOP’s

This tutorial category contains all the tutorials related to Object-Oriented PHP.

Access Modifiers

In this tutorial, we will learn about Access Modifiers. Access Modifiers are used to control the accessibility of properties and methods. For example, you want to create a method that you do not want to make accessible from outside of the class. You can easily do this with the help of Access Modifiers. Types of Access Modifiers:  There are three types of Access Modifiers in PHP, which are as follow.  public protected private Public:  This is the default access modifier in PHP. In case you do not write any access modifier with the method or property default access modifier will be there by default. Any property or method with a default access modifier can be accessed from everywhere.  <?php class Example { //variable with default access public $var = “Python”; } $obj = new Example(); echo $obj->var; ?> Protected:  A method or property with protected access modifier can be accessed from with the class or in classes that are inheriting to that particular class. To apply a protected access modifier on your property or method you have to write a protected keyword along with an identifier. To understand the protected access modifier please check the following example program. <?php class Example { //variable with protected access protected $var = “Owlbuddy Programming Tutorials”; function show(){ echo $this->var; } } $obj = new Example(); //will work Normally $obj->show(); //will throw an Error (Please Uncomment following line to check) //echo $obj->var; ?> Private: Any method or property with a private access modifier can be accessed only inside the class where it is defined. Please check out the following program to understand it. <?php class Example { //variable with private access private $var = “Owlbuddy Programming Tutorials”; function show(){ echo $this->var; } } $obj = new Example(); //will work Normally $obj->show(); //will throw an Error (Please Uncomment following line to check) //echo $obj->var; ?>   Parvesh SandilaParvesh Sandila is a passionate web and Mobile app developer from Jalandhar, Punjab, who has over six years of experience. Holding a Master’s degree in Computer Applications (2017), he has also mentored over 100 students in coding. In 2019, Parvesh founded Owlbuddy.com, a platform that provides free, high-quality programming tutorials in languages like Java, Python, Kotlin, PHP, and Android. His mission is to make tech education accessible to all aspiring developers.​ new.owlbuddy.com

Access Modifiers Read More »

PHP Destructor

In this tutorial, we will learn about PHP Destructor. The destructor method is totally reverse of the constructor method. PHP destructor does not return any value. In case we create __destructor function in class, PHP will automatically call the destructor function if there are no other references to a particular object or at the end of the script. Please Note, that the desructor function starts with two underscores (__). for e.g. __destruct() Here an example program to show how to define destructor in PHP. <?php class Example { //defing a constructor function __construct() { echo “This is constuctor method.<br>”; } //defing a normal method function show(){ echo “This is show method.<br>”; } //defing a destructor function __destruct() { echo “The is destructor method.<br>”; } } $obj = new Example(); $obj->show(); $obj->show(); $obj->show(); ?> The destructor will be called even if script execution is stopped using exit() or die() function. Difference b/w Constructor and PHP Destructor: Constructor Destructor It Allocates Memory It Deallocates Memory Get called at the time of class is instantiated Get called at the time of deletion of the object Name of function is _construct(). Name of function is _destruct() Can accept parameters  Can not accept parameters  Constructors can be overloaded  Destructors can not be overloaded   Parvesh SandilaParvesh Sandila is a passionate web and Mobile app developer from Jalandhar, Punjab, who has over six years of experience. Holding a Master’s degree in Computer Applications (2017), he has also mentored over 100 students in coding. In 2019, Parvesh founded Owlbuddy.com, a platform that provides free, high-quality programming tutorials in languages like Java, Python, Kotlin, PHP, and Android. His mission is to make tech education accessible to all aspiring developers.​ new.owlbuddy.com

PHP Destructor Read More »

PHP Constructors

In this tutorial, we’ll explore the PHP Constructors, a special function within a class. Its primary purpose is to initialize the properties of each object instance. The constructor is remarkable because it’s automatically invoked when a new object is created. Let’s delve into the details of this essential feature. Please Note, that the construct function starts with two underscores (__). Here you can check an example program to understand how to create a constructor in PHP: Types of Constructor: Default Constructor:  A constructor with no parameters is referred to as the default constructor. While it can be used to initialize class properties, it also allows for calling other member functions within the class. This flexibility enhances the default constructor’s utility in managing class behaviour. Parameterized Constructor: A constructor with parameters is called a parameterized constructor. This allows us to provide different values to initialize the data members of the class. The example program below illustrates how to use a Parameterized PHP Constructor, making it easier to grasp the concept. Copy Constructor: The copy constructor is used to create a duplicate of an already existing object. It accepts the address of another object as a parameter, allowing for the creation of an identical copy. Parvesh SandilaParvesh Sandila is a passionate web and Mobile app developer from Jalandhar, Punjab, who has over six years of experience. Holding a Master’s degree in Computer Applications (2017), he has also mentored over 100 students in coding. In 2019, Parvesh founded Owlbuddy.com, a platform that provides free, high-quality programming tutorials in languages like Java, Python, Kotlin, PHP, and Android. His mission is to make tech education accessible to all aspiring developers.​ new.owlbuddy.com

PHP Constructors Read More »

PHP Classes and Objects

In this tutorial, we will learn about PHP Classes and Objects. As we know Object-oriented programming is a programming paradigm, based on the concept of classes and objects. In very easy language a class is the blueprint of the object and an object is a run time instance of that class. Let's check how to define a class and create an object in PHP. Defining a class: In PHP, the class keyword is used to define a class. We write the class keyword followed by the class name and after that, we define the properties and methods of the class in curly braces {}. Please check out the following example program to understand this. <?php class Student { // proerties and methods will come here… } ?> In the above example program, you can see how we have created a Student class. For good coding practice always write class name starting from captial letter.  For e.g. Student, Person, Car etc. We can define properties and methods in the class. Please check out the following program. <?php class Student{ // Properties public $name; public $age; // Methods function set_properties($name, $age) { $this->name = $name; $this->age = $age; } function get_properties() { echo $this->name.’ ‘.$this->age; } } ?> Creating an Object in PHP: An object is a run time instance of the class. We can create multiple objects of any class and each object will have all the properties and methods defined in the class. Each object will occupy a different space in memory and each object have the ability to set different values for properties. In PHP we use the new keyword to create objects of the class. Please check out the following example program to understand the concept of objects wisely. <?php class Student{ // Properties public $name; public $age; // Methods function set_properties($name, $age) { $this->name = $name; $this->age = $age; } function get_properties() { echo $this->name.’ ‘.$this->age; } } //creating objects using new keyword $student1= new Fruit(); $student2 = new Fruit(); //calling to set properties method of Student class $student1->set_properties(‘Rahul’,19); $student2->set_properties(‘Mohit’,20); //calling to get properties method of Student class echo $student1->get_properties(); echo “<br>”; echo $student2->get_properties(); ?>   Parvesh SandilaParvesh Sandila is a passionate web and Mobile app developer from Jalandhar, Punjab, who has over six years of experience. Holding a Master’s degree in Computer Applications (2017), he has also mentored over 100 students in coding. In 2019, Parvesh founded Owlbuddy.com, a platform that provides free, high-quality programming tutorials in languages like Java, Python, Kotlin, PHP, and Android. His mission is to make tech education accessible to all aspiring developers.​ new.owlbuddy.com

PHP Classes and Objects Read More »

What is PHP OOPs

In this tutorial, we will learn about What is PHP OOPs. OOP stands for Object-Oriented Programming. Object-oriented programming is a programming paradigm based on the concept of classes and objects. By using the OOPs approach we can divide the software program into simple, reusable pieces of code(known as a class). The classes are used to create individual instances of objects. Difference between Class and Objects: Class is like the blueprint to create instances of the object and we can create multiple instances of a class. Please check out the following illustration to understand the concept of class and objects more wisley. Important Object-Oriented Concepts: Class  Object Member Variable  Member function  Inheritance Polymorphism Encapsulation Inheritance Parvesh SandilaParvesh Sandila is a passionate web and Mobile app developer from Jalandhar, Punjab, who has over six years of experience. Holding a Master’s degree in Computer Applications (2017), he has also mentored over 100 students in coding. In 2019, Parvesh founded Owlbuddy.com, a platform that provides free, high-quality programming tutorials in languages like Java, Python, Kotlin, PHP, and Android. His mission is to make tech education accessible to all aspiring developers.​ new.owlbuddy.com

What is PHP OOPs Read More »

Scroll to Top
×