আগামী ১৫ ডিসেম্বর -২০১৭ তারিখ থেকে শুরু হচ্ছে পাঁচ মাস ব্যাপী Professional Web Design and Development with HTML, CSS PHP,MySQL,JavaScript, AJAX, JQUERY, Bootstrap and Live Project কোর্সের ৮৭ তম ব্যাচ এবং ২৬ ডিসেম্বর-২০১৭ তারিখ থেকে শুরু হচ্ছে চার মাস ব্যাপী Zend PHP-7 Certified PHP Engineering (Advance PHP) কোর্সের ৩৫ তম ব্যাচ। প্রত্যেকটি কোর্স এর ফী নির্ধারণ করা হয়েছে ৩০,০০০/= আগ্রহীদেরকে অতিসত্বর মাসুদ আলম স্যার এর সাথে যোগাযোগ করতে অনুরোধ করা যাচ্ছে। স্যার এর মোবাইল: 01722 81 75 91

PHP Object Oriented Programming Part-2: Making and Using Class, Object and Class Members

What is the rule of declaration a class in PHP?

class-object

In PHP, it is required to start a class name by letter or by underscore after the “class” keyword. After class name, we need to put a curly braces to start the class, and curly braces to finish the class. The inner part of the curly braces is called class body. PHP’s reserved word can not be given as class name. Let’s take a look at an eample:

<?php
class SimpleClass // class Declaration 
{ //class start
 
  /*..........
    Class Body
    .........*/
     
} //class end
?>

What is the rule of creating an object from a class in PHP?

To create an object from class in PHP, new keyword must be given prior to class name outside the class. That means new className or new className(). And the object that is placed in the variable is called an object handler. See the example below:

<?php
class SimpleClass
{ //class start
 
  /*..........
    Class Body
    .........*/
     
} //class end
 
$objHandler=new SimpleClass(); // Object Call
?>

What is the rule of declaration of constant, property and method in class?

To declare constant, property and method in class, the names of constant, property and method are required to give any one visibility to your needs. But the visibility of the constant is optional. If you do not declare any visibility at constant, then the default is public. See the example below:

<?php
class SimpleClass
{
    public $var = 'a default value'; // public property declaration
    private const a="America"; // private constant Declaration
 
    // public method declaration
    public function displayVar() {
        echo $this->var;
    }
}
?>

What are the rules of use property, method and constant of the class in PHP?

Properties, method, and constant in class can be used in two ways.

  1. Outside of class
  2. Inside of class

Using Properties, method, and constant outside of class

The property, method and constant in class Can be used in two ways outside of the class.

  • Using object handler or object variable: In PHP, any public non-static property and public non-static method within the class can be used for any purpose outside of the class by using the object handler or the object variable. See the example below:

    <?php
    class SimpleClass
    {
        // property declaration
        public $var = 'Hello Simple Class';
     
        // method declaration
        public function displayVar() {
            echo $this->var;
        }
    }
    $obj=new SimpleClass;
    $obj->displayVar(); 
    /*
    access class method using Object Variable $obj
     Result: Hello Simple Class
    */
    ?>
    
  • Using “::” scope resolution operator: In PHP, if you want to use any public static property, public static method and public constant of any class outside of that class, then you can use “:: scope resolution operator”. See the example below:

    <?php
    class SimpleClass
    {
        // property declaration
        public $var = 'Hello Simple Class';
    public const a="America";
     
        // Static method declaration
        public static function sayHello() {
            echo "Hello World";
        }
    }
    SimpleClass::sayHello();
    echo SimpleClass::a;
    /*
    access class method using directly class name and scope resolution Operator ::
     Result: Hello World
    */
    ?>
    

Use property, method and constant within the class.

Property, method and constant can be used in two categories within the class.

  • Using “$this” pseudo-variable: See the example below:

    <?php
    class SimpleClass
    {
        // property declaration
        public $var = 'Hello Simple Class';
     
        //  method declaration 
        public function sayHello() {
            echo $this->var; // use $this to show $var value
        }
    }
    $obj=new SimpleClass;
    $obj->sayHello(); // Result: Hello Simple Class
    ?>
    
  • Using “::” scope resolution operator: If you want to use any static property, static method and constant within the class, then use “:: scope resolution operator” for any purpose. See the example below:

    <?php
    class SimpleClass
    {
        // property declaration
       static public $var = 'Hello Simple Class';
       const a="America";
     
        //  method declaration 
        public function sayHello() {
            echo self::$var; // use self with scope resolution Operator
            echo "<br>", self::a;
        }
    }
    $obj=new SimpleClass;
    $obj->sayHello();
     
    ?>
    

Hi, My name is Masud Alam, love to work with Open Source Technologies, living in Dhaka, Bangladesh. I’m a Certified Engineer on ZEND PHP 5.3, I served my first five years a number of leadership positions at Winux Soft Ltd, SSL Wireless Ltd, Canadian International Development Agency (CIDA), World Vision, Care Bangladesh, Helen Keller, US AID and MAX Group where I worked on ERP software and web development., but now i’m a founder and CEO of TechBeeo Software Company Ltd. I’m also a Course Instructor of ZCPE PHP 7 Certification and professional web development course at w3programmers Training Institute – a leading Training Institute in the country.

Leave a Reply

Your email address will not be published. Required fields are marked *