PHP oops - What is Class?

PHP class is collection of objects. Classes are the blueprints of objects.PHP Class includes local methods and local variables.

You can define your own class by starting with the keyword ‘class’ followed by the name that you want to give your new class.

PHP Syntax:

class class-name { }

PHP Example :

<?php
    class fruits {
                  

            var $name1;
            var $name2 = "constant string";

            function getFruits($param) {
                    //function code to be executed.
           }


    }
?>

A set of braces can contains any number of local variable declarations and function definitions.

Variable declarations start with the special form var which is followed by a conventional $ variable name. Also you can assign constant value to local variable of class.

PHP functions defined in class are local to that class and that functions can be accessed using object of class.

 

 

Comments

Leave a Reply

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

86031