Class & Object
Class

Class is a user define datatype. Means we define variable and function inside class. Now look how to create class in PHP.

Syntax:-

class class_name
{

private variable;
protected variable;
public variable;

private function;
protected function;
public function ;

}

here class is keyword and class_name is specify by coder. every class member(like variable,function) may be private, protected and public. these(private,protected and public) are called as visibility

private :- we can access private member inside class only. like we access function and variable in class member function.

public :- we can access public member outside the class. like another class member function can be access any class member.

protected :- protected member is available outside the class. Means in another class member function can access it. but directly object we can't access protected member. but this is possible with public member.

Note:- if we are not declare any visiblity to any class member so member of class is public by default.

Example 1:- A simpe class with one function

<?php
class demo
{
public function show()
{
echo "This is class function";
}
}
?>

Object

Object is the instance of the class. Means without object class is nothing. So object is must to be create. When we create object the instance of the class is created. and by object we can access class member function. Now look how to create object.

we create object just like we create variable but for create object, memory allocation is necessary.
example:-

$obj=new demo();

where $obj is a object of the demo() class. and new operator is allocating memory for object.

Example 1:- In this example we simply create class and create object for that class and call class member function.

<?php
class demo
{
public function show()
{
echo "This is class function";
}
}

$obj=new demo();
$obj->show();
?>

Output:-
This is class function

Note :- By default class member is public if we are not write public infont of show function yet this code work properly

Example 2:- A code to use of private

<?php
class demo
{
private function show()
{
echo "This is class function";
}
}

$obj=new demo();
$obj->show();
?>

Output:-
Fatal error: Call to private method demo::show() from context '' in C:\xampp\htdocs\www\php_function\class6.php on line 11

Note:- any private and protected not accessable through object. Only class member function can use private member.

Example 3:- PHP Script to use of class variable

<?php

class demo
{
private $a=3;
function display()
{
echo $this->a;
}
}

$obj=new demo();
$obj->display();

?>

Output:-
3

Note:- $a is public variable. In a class every variable must be start with var or private or public .

Example 4:- In this we just passing value to the class member function. and class member function print the value.

<?php

class demo
{
var $a=23;
function display($k)
{
$this->a=$k;
echo $this->a;
}
}

$obj=new demo();
$obj->display(4);

?>
Output:-
3

Example 5:- In this example we add two value through class member function.

<?php

class demo
{
function display($x,$y)
{
$this->a=$x;
$this->b=$y;
$this->c=$this->a+$this->b;
echo $this->c;
}
}

$obj=new demo();
$obj->display(4,5);

?>

Output:-
9

This is not necessary to declare variable. when we use variable then variable automatically declared.

Example 6:- In this example we just pass two value to function and function return the addition of two value.

<?php

class demo
{
function display($x,$y)
{
$this->a=$x;
$this->b=$y;
$this->c=$this->a+$this->b;
return $this->c;
}
}

$obj=new demo();
$result=$obj->display(8,5);
echo $result;

?>

Output:-
13

Example 7:- Another way to access class member

<?php
class demo
{
public function show()
{
echo "This is class function";
}
}

demo::show();
?>

Note:- here show function is common for all object for the demo class.

Static variable and Static function

when a class member(variable and function) declared as static then we can't be accessed by class object.
It can access by class name.

Code to access static member.

demo::$a=45;

demo::show();

where demo is class name

Note:

static member functionality same in all objects of the class.

suppose we have 10object of the class then static member is same for all class whereas nonstatic member is different

Example 8:-

<?php
class demo
{
    public static $a;
     public static function show() {
        echo self::$a;
       
    }
}


demo::$a=45;

demo::show();

?>


Previous
Next