Thursday, July 4, 2024
HomeLanguagesPhpPHP | Classes

PHP | Classes

Like C++ and Java, PHP also supports object oriented programming

  1. Classes are the blueprints of objects. One of the big differences between functions and classes is that a class contains both data (variables) and functions that form a package called an: ‘object’.
  2. Class is a programmer-defined data type, which includes local methods and local variables.
  3. Class is a collection of objects. Object has properties and behavior.

Syntax: We define our own class by starting with the keyword ‘class’ followed by the name you want to give your new class.

<?php 
    class person {
 
    }
?>

Note: We enclose a class using curly braces ( { } ) … just like you do with functions.

Given below are the programs to elaborate the use of class in Object Oriented Programming in PHP.
The programs will illustrate the examples given in the article.

Program 1:




<?php
class neveropen
{
    // Constructor
    public function __construct(){
        echo 'The class "' . __CLASS__ . '" was initiated!<br>';
    }
      
}
   
// Create a new object
$obj = new neveropen;
?>


Output:

The class "neveropen" was initiated.

Program 2:




<?php
class neveropen
{
    // Destructor
    public function __destruct(){
        echo 'The class "' . __CLASS__ . '" was destroyed!';
    }
      
}
   
// Create a new object
$obj = new neveropen;
?>


Output:

The class "neveropen" was destroyed.

Reference:
Classes in PHP

Nicole Veronica Rubhabha
Nicole Veronica Rubhabha
A highly competent and organized individual DotNet developer with a track record of architecting and developing web client-server applications. Recognized as a personable, dedicated performer who demonstrates innovation, communication, and teamwork to ensure quality and timely project completion. Expertise in C#, ASP.Net, MVC, LINQ, EF 6, Web Services, SQL Server, MySql, Web development,
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments