PHP Tutorial Tutorials - PHP Class and Object

PHP Class and Object

What is a class?

A class is simply a representation of a type of object. It is the blueprint, or plan, or template, that describes the details of an object.

In PHP,A class is a blueprint for creating an object.
A class can be declared using the class keyword, followed by the name of the class and a pair of curly braces ({})

<?php
class House
{
    // code
}
?> 

Naming a Class

  • The name you use for a class should not be a PHP reserved word.
  • Class names conventionally are written in PascalCase i.e. each concatenated word starts with an uppercase letter (e.g. MyClass,CourtyardHouse).

What is an Object?

An object is an instance of a class.
A class is the blueprint from which the individual objects are created.
We can build more than one object from a class.

  • Everything is an object.
  • An object can be anything.
  • An object has state and behavior.

In PHP, The new keyword create a new instance(object) of the class,

<?php
    $house1 = new House();
    $house2 = new House();
?> 

The process [create object from a class] is called instantiation.

Date:2019-10-09 00:22:49 From:www.Lautturi.com author:Lautturi