Chapter 3 -Class and Object
Chapter 3 -Class and Object
Class : A class is a template for objects. When a class is defined, it includes defining the names
of its properties and code for its methods. Encapsulation is the idea that a class provides certain
methods to the code that uses its objects, so the outside code does not directly access the data
structures of those objects.
Defining a class :
A class is defined by using the class keyword, followed by the name of the class and a pair of
curly braces ({}). All its properties and methods go inside the curly brackets.
Syntax :
<?php
class classname [extends baseclass][implements interfacename,[interfacename,…]]
{
[visibility $property [=value];…]
[function functionname(args) { code }…] // method declaration & definition
}
?>
Example :
<?php
class student
{
Properties/attributes
Methods/functions
}
?>
Using access modifiers(visibility mode), you can change the visibility of methods.
Methods/properties that are accessible outside should be declared public. Methods/properties on
an instance that can only be called by methods within the same class should be declared private.
Finally, methods/properties declared as protected can only be called from within the object’s
class methods and the class methods of classes inheriting from the class. Defining the visibility
of class methods is optional; if a visibility is not specified, a method is public.
Declaring Properties : Class member variables are called properties. Sometimes they are
referred as attributes or fields.The properties hold specific data and related with the class in
which it has been defined.Declaring a property in a class is an easy task, use one of the keyword
public, protected, or private followed by a normal variable declaration.
Example :
<?php
class student
{
public $name;
public $rollno;
}
?>
Declaring Methods :
A method is a function defined inside a class. A valid method name starts with a letter or
underscore, followed by any number of letters, numbers, or underscores.The method body
enclosed within a pair of braces which contains codes. The opening curly brace ( { ) indicates the
beginning of the method code and the closing curly ( } ) brace indicates the termination of the
method.If the method is not defined by public, protected, or private then default is public.Can
access properties and methods of the current instance using $this (Format $this->property) for
non static property
Example:
<?php
class student
{
public $name;
public $rollno;
function accept($name,$rollno)
{
$this->name=$name;
$this->rollno=$rollno;
}
}
?>
Object : An object is an instance of class.The data associated with an object are called its
properties. The functions associated with an object are called its methods. Object of a class is
created by using the new keyword.
Properties and functions(methods) declared inside class with public/default visibility mode can
be access outside class using object of a the same class.
Example :
<?php
class student
{
public $name="abc"; //initialize property with default value
function display() // by default function is public
{
echo $this->name; // this is a pointer that points to calling object of function (s1)
}
}
$s1=new student(); // create new object.
echo $s1->name; // accessing property
$s1->display(); // calling method/function from class
?>
Output : abcabc