PHP7 Class

A class is a collection of objects of similar type. For example, car, bus, bicycle and truck are members of the class vehicle. Once a class has been defined, we can create any number of objects belonging to that class.


The syntax of declaring a class

The class definition begins with the keyword class, followed by a class name and encapsulated all code within the curly brackets {}. Class names cannot include spaces, instead of space we can use one underscore (_) to connect two words, like set_name. It should be begin with a letter, then can be any combination of letter and number.

<?php
class Demo { 

}
?>

In the above example, we have created a class name Demo. It is important to keep aware of opening and closing brackets.


PHP7 Object

An object is an instance of the class. In Object, data members and properties are bundled together into a single entity. A single application can contain one or many numbers of objects. The properties and methods of a class are unless until an instance of a class is created.


Creating an instance of a class

Class is a template for creating an object. We can create one or more than one objects of a class. In PHP, object of a class is created by using the new operator. So, with the help of new keyword an instance of the class is created in the memory.

<?php
    class Demo {

    }
    $obj1 = new Demo();
    $obj2 = new Demo();
?>

In the above example, we have created two objects $obj1 and $obj2 of the same type of the Demo class. In PHP, each generated object has their own identity.


To get identity of an object

<?php
    class Demo {

    }
    $obj1 = new Demo();
    $obj2 = new Demo();
    var_dump($obj1);
    var_dump($obj2);
?>

The output of the above code

object(Demo)#818 (0) { } object(Demo)#816 (0) { }




PHP7 Properties and Methods

The variable defined within a class is called property, a property is defined to store some data. So when a property is created, we must also include a data type to describe the kind of property. Properties include characteristics of a class. The property of a class reserves some space in the memory. The function declared within a class is called method. A method is declared to perform some task.

<?php
    class Company {
        // Properties
        $title = 'etp'; 
        $location = 'Delhi';

        // Method
        function welcome() {
            print 'Welcome to the ETP Company';
        }
    }
?>

In the above example, the class Company has set two properties and one method. We can access this property and method outside of the class by generating an object and with the help of '->' character.

Example

<?php
    class Company {
        // Properties
        public $title = 'ETP'; 
        public $location = 'Delhi';

        // Method
        function welcome() {
            print "Welcome to the ".$this->title." Company.";
        }
    }
    $obj = new Company();
    echo $obj->title;
    echo '<br/>';
    echo $obj->location;
    echo '<br/>';
    echo $obj->welcome();
?>

Output of the above code

ETP
Delhi
Welcome to the ETP Company.

In the above example, $this refers to the current object.







Read more articles


General Knowledge



Learn Popular Language