cramerz logo

Php Method Property


In PHP Object Oriented Programming (OOP) a function that is defined inside a class, known as Method. Similarly a variable defined inside class is called Property.

How to create Property and Method in PHP

The declaration of a property in PHP class is same as we create variables in PHP, it's preceeded by a keyword var. A value can be assigned to property exactly in the same way as we assign value to a variable. A simple Syntax of Property Declaration in PHP Class is given here

var $property_name = "Property Value";

To use the property of class, we need to associate it to object of class by using indirection operator -> 

//class declaration
class className
{

  var $property_name = "Property Value";  

}

//object creation
$obj = new className();

//use the class property with it's object
$obj -> property_name;


Notice that when using a class property name with object dollar sign $ is not used with property name. Now lets have a look at a practicle example of PHP Class with a property.

<?php

//class declaration
class vehicle
{
 
  //Class Property
  var $wheel = 4;

}

//object of Class vehicle
$car = new vehicle();

//use property of class vehicle
echo $car -> wheel;

//above script will print 4

?>

 

 



all rights reseverd by Cramerz.com©. web design & web development by DotPeak. hosting by HostBay
all contents on cramerz are the property of Cramerz.com. web design courses