Skip to content

PHP Classes & Functions

Basics

Class and Function

Class is an instance of an object. Every class begins with the keyword class followed by the class name. The class name can be any name that isn’t a reserved word in PHP. Th class contains the definition of the class members and methods.

For example, lets try to describe this blog. The Class “myBlog” might have function like PHP, javascript, mysql and ajax. The functions might have variables such as version and frameworks.

Example

<?php

class myBlog()
{
    function php($php_version)
    {
        echo "PHP Version:".$php_version;
    }

    function mysql($mysql_version)
    {
        echo "MySql Version:".$mysql_version;
    }

    function javaScript($js_framewrk)
    {
        $output = "Javascript frameworks";

        for($i=0;$i<count($js_framewrk);$i++)
        {
            $output .= "$js_framewrk[$i]";
        }

        echo $output;
    }

    function ajax($ajax_framewrk)
    {
        echo "Ajax Framework:".$ajax_framewrk;
    }
}

$php_version = "5.0";
$mysql_version = "6.0";
$js_framewrk = array("jquery", "mootools", "scriptaculous",
"prototype", "yui");
$ajax_framewrk = "scriptaculous";

$object = new myBlog();
$object->php($php_version);
$object->mysql($mysql_version);
$object->javaScript($js_framewrk);
$object->ajax($ajax_framewrk);

?>

As said, In the above example, it has a class called myBlog and the class has four function php, mysql, js, ajax. Finally i’ve created the object for the class myBlog ($object = new myBlog();), then i’ve called the members one by one. we can also call the function in scope resolution method (i.e.) without creating the object as,

<?php


   $output = myBlog::php($php_version);

?>

Visibility

The visibility of a property or method can be defined by prefixing the declaration with keywords called public, private and protected. As a Programmer you should aware of protecting methods and members from unwanted access.

Public: Anyone inside the class or outside can access them (i.e.) it can be accessed everywhere.

Private: Visible only to the class that defines the item (i.e.) only specified class can access them, even subclasses will deny the access.

Protected: Only the specified class and its subclass. (i.e.) access to inherited and parent class(specified class).

Members Visibility

Example

<?php

class MyBlog
{
    public $javascript = 'Public';
    protected $php = 'Protected';
    private $my_sql = 'Private';

    function showSpecifiers()
    {
        echo $this->javascript."<br>";
        echo $this->my_sql."<br>";
        echo $this->php;
    }
}

$obj = new MyBlog();
echo $obj->javascript; // Works
echo $obj->my_sql; // Fatal Error
echo $obj->php; // Fatal Error
$obj->showSpecifiers(); // Shows Public, Protected and Private

?>

<?php

class wordPress extends myBlog
{
    protected $php = "protected_again"; /*We can declare the public
    and protected method but not private.*/

    function showSpecifiers()
    {
        echo $this->javascript."<br>";
        echo $this->my_sql."<br>";
        echo $this->php;
    }
}

$obj2 = new wordPress();
echo $obj2->javascript; // Works
echo $obj2->my_sql; // Undefined
echo $obj2->php; // Fatal Error
$obj2->showSpecifiers(); /* Shows Public, Undefined and
Protected_again*/

?>

Method Visibility

Example

<?php

class myBlog
{
    public function javaScript()
    {
        echo "public method<br>";
    }

    protected function php()
    {
        echo "protected method<br>";
    }

    private function mySql()
    {
        echo "private method";
    }

    /*without access specifier, as default method is
considered as public*/
    function showSpecifiers()
    {
        $this->javaScript();
        $this->php();
        $this->mySql();
    }
}

$obj = new myBlog();
$obj->javaScript(); //works
$obj->php(); //fatal error
$obj->mySql(); //fatal error
$obj->showSpecifiers(); /*prints public method protected method
private method*/

?>

<?php

class wordPress extends myBlog
{
    function showSpecifiersAgain()
    {
        $this->javaScript();
        $this->php();
        $this->mySql(); //fatal Error
    }
}

$obj2 = new wordPress();
$obj2->javaScript(); //works
$obj2->showSpecifiersAgain(); /*Public and Protected work,
not Private*/

?>

Constructor

A function becomes a constructor when it has the same name as the class (i.e.) the class and function have the same name. They automatically called when you create a new instance of class with new.

Example

<?php

class myBlog
{
    function myBlog()
    {
        echo "This is a Blog";
    }
}

$variable = new myBlog(); //no need to call the method, on creating
instance the method will be called.

?>

<?php

class sampleA
{
    function sampleA()
    {
        echo "constructor of sampleA"
    }

    function sampleB()
    {
        echo "regular function";
    }

}

class sampleB extends sampleA
{
    function sampleC()
    {
        echo "regular function";
    }
}

$variable = new sampleB(); // This will call function sampleB
as constructor

?>

<< Back

PHP Regular Expressions

Advertisements
2 Comments
  1. Respected sirs/mam,

    plz provide me the coding for openinviter.php and xvf.php of openinviter.com

  2. Aravind permalink

    You can get the example source code frm this link,
    http://www.openinviter.com/download.php

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: