Class and Object in PHP


Prev Tutorial Next Tutorial

Class and Object in PHP

Class: This is a programmer-defined data type, which includes local functions as well as local data. You can think of a class as a template for making many instances of the same kind (or class) of object.

In PHP declare a class using the class keyword, followed by the name of the class and a set of curly braces ({}).

Syntax Create Class in PHP

<?php

class MyClass
 {
   // Class properties and methods go here
 }

?>
After creating the class, a new class can be instantiated and stored in a variable using the new keyword:

Syntax

$obj = new MyClass;

In PHP, to see the contents of the class, use var_dump(). The var_dump() function is used to display structured information (type and value) about one or more variables.

Syntax

var_dump($obj);

Class and Object in PHP

<?php
 
class MyClass
{
    // Class properties and methods go here
}
 
$obj = new MyClass;
 
var_dump($obj);
 
?>

Object: An individual instance of the data structure defined by a class. You define a class once and then make many objects that belong to it. Objects are also known as instance.

Syntax Class in PHP

<?php

class phpClass 
 {
  var $var1;
  var $var2 = "constant string";
      
  function myfunc ($arg1, $arg2) 
 {
     [..]
  }
     [..]
 }

?>

Syntax Create Object in PHP

<?php

class MyClass
{
    // Class properties and methods go here
}
 
$obj = new MyClass;
 
var_dump($obj);

?>

Prev Tutorial Next Tutorial

Advertisements
Download Projects

Advertisements

Download WhatsApp Status Video

Buy This Ad Space Only $50 per Month, Ad Size 300X250 Contact on: [email protected] WhatsApp no. 8882538826

Google Adsense Advertisements