Class and Object in PHP

Advertisements

Switch Case in PHP Access Specifier in PHP

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);

?>

Switch Case in PHP Access Specifier in PHP

Google Advertisment

Buy This Ad Space @$20 per Month, Ad Size 600X200 Contact on: hitesh.xc@gmail.com or 9999595223

Magenet is best Adsense Alternative here we earn $2 for single link, Here we get links ads. Magenet

For Projects 9999595223

Google Advertisements


Buy Websites 9999595223

Buy College Projects with Documentation Contact on whatsapp 9999595223. Contact on: hitesh.xc@gmail.com or 9999595223 Try this Keyword C++ Programs

Advertisements