Posts

Showing posts with the label Object Oriented PHP

Creating a constructor in PHP class

Sometimes you need to initialize class object before call them. So before using our object we can initialize our object using PHP constructor. PHP constructor just initialize object, not allocate object. To allocate object we use new keyword. Step to write constructor: Write function then writes double underscore __ and then write name constructor.   See the example below function __constructor() { } Constructor body is enclosed by curly braces. Now we write a constructe for Myclass <?php class Myclass{ function __constructor() { echo “This is PHP constructor”; } } $obj=new Myclass(); ?> See the output. One important thing that in this class we just create a new object of this class but it print “This is PHP constructor”  because we initialize object before use. 

Declaring variable and method in PHP class

We knew  that variable is an object state. We need to declare all object state within PHP class.  We want to declare a variable within class such this way class Myclass{ public $variable; } Add string in that variable class Myclass{ public $variable= “Storing Variable Data”; } Declaring Method:: A method is an object behavior. Object behavior defines object task. So we want to print the variable data in PHP method. To do this at first write function then write method name with a pair of parenthesis () and body with curly braces {} class Myclass{ public $variable=”Storing variable data”; public function display_variable() { // method body } } Access Modifier:: PHP access modifier used to take control of the class or class properties. At this moment we only consider public and private access modifier others will be discussed later. public modifier —the field is accessible from all classes. private modifier —the field is accessible only within its own class. This access modifier ...

A Simple PHP class

Let’s start our thinking of the class with a simple PHP class example. In this example we want to print a variable value. <?php class SimpleClass {     // property declaration     public $variable = 'This is state of an PHP object';     // method declaration     public function display_variable() {         echo $this->variable." Under PHP class Method";     } } ?> Save the code and run it. This program print nothing. It is important to remember that a class declaration only creates a template, it does not create the actual object. To print the variable value you need to create a instance of this class. Creating an instance You can create an instance using new operator. A class must be created before instantiating object. So the whole code is <?php class SimpleClass {     // property declaration     public $variable = "This is state of an...

PHP class

Class is a core concept of PHP Object Oriented Programming. We can say that class is a template for an object and object is an instance of a class. We use object and instance as the same thing. For  example, Human class, Bycicle class etc. The General Form of a class We use class keyword to define a class. So we can define PHP class by following way class classname{ //Object state // Object bahavior } A PHP class body is  enclosed by curly braces. PHP class naming convention Class body surrounded by curly braces {} Class name starts with capital letter(It is not necessary but we use this convention in our tutorial) We write all methods and variable within class.

What is an object in PHP

Basic understanding about PHP object   Before entering into the depth of PHP Object Oriented Programming concept at first we need to understand what is an object in PHP. The object is nothing but it’s a real world entity. For example, a human, your dog, your bicycle, your desk everything are real world object. Every object has two attributes state and behavior. A human has many state and behavior such as Human state: hands, legs, mouth etc. Human behavior: moving hands, walking leg etc. You can hone your skill by identifying real world objects with state and behavior. Think about your table lamp. It has two state off and on and has two behavior turning off and turning on. In PHP object oriented programming we use state as a variable and behavior as a method. Object oriented method is actually a PHP function and within function we use variable that is actually object state. A method perform specific works by combining it's internal state.Using methods we hide internal state from th...