Constructor of Classes and Objects in OOP PHP

A constructor is a special functions which are automatically called when an object is created . The constructor is the most useful , especially because it allows you to send parameters along when creating a new object, which can then be used to initialize variables on the object. Constructor is nothing but a function defined in your PHP class. Constructor function automatically called when you will create object of the class. As soon as you will write $object = new yourClass() your constructor function of the class will be executed. you can create constructor by defining magic function __construct.

Example:

class MyClass 
{
        public $var;

    // Class constructor
    public function __construct($var)
    {
        echo 'Created an object of MyClass';
        $this->var = $var;
    }

}
// Make an object
$objA = new MyClass('A');

// Call an object method to show the object’s property

If you have created parameter in the constructor you need to pass value for them on the time of object creation.
$objCls = new MyClass(‘A’). If you will not send value PHP will throw error.

Read more OOP in PHP

I hope its helpful for every one, contact & ask any question on http://fb.com/smartwebcareindia

How to Write Class in OOP PHP ?

This instructional exercise, we will begin from the essential idea of classes . Class is something which characterizes your article. Class speaks to all properties and practices of item. Presently give us a chance to take a sample of the programming. Your advantage adding machine article is case of class interest adding machine. Interest adding machine class characterizes properties like capital rate, and conduct like basic interest count and self multiplying dividends estimation.

In PHP making of class is extremely basic. You can make class utilizing label class. In class piece you can characterize your properties as class variable and capacity as class conduct. Classes and Objects are key a portion of item situated programming

Example:

class MyClass 
{
        public $var;

    // Class constructor
    public function __construct($var)
    {
        echo 'Created an object of MyClass';
        $this->var = $var;
    }

    public function show_var()
    {
        echo $this->var;
    }
}
// Make an object
$objA = new MyClass('A');

// Call an object method to show the object's property
$objA->show_var();

// Make another object and do the same
$objB = new MyClass('B');
$objB->show_var();

Read more OOP in PHP

I hope its helpful for every one, contact & ask any question on http://fb.com/smartwebcareindia

Object in OOP PHP

This tutorial series is for both beginners and middle level programmer who want to learn advance concept of OOP in PHP from basic . In this series we will explore all aspect of OOP in PHP from beginning. Object oriented programming is a technique to design your application. Application could be any type like it could be web based application, windows based application. OOP is a design concept. In object oriented programming, everything will be around the objects and class. By using OOP in PHP you can create modular web application. By using OOP in PHP we can perform any activity in the object model structure. In this article we are going explore exactly what OOP is in relation to PHP, and look at a few things you should remember about it as well. We’ll end of with a simple example of how to use it.

What is Object?

PHP is an object-oriented programming language, which means that you can create objects, which can contain variables and functions. When talking about objects, you refer to variables belonging to these objects as properties (or attributes or fields), and functions are called methods. Object in programming is similar to real word object. Every programming object has some properties and behaviours. Your car has property (colour, brand name) and behaviour(it can go forward and backward). If you are able to find properties and behaviours of real object. Then it will be very easy for you to work with Object Oriented Programming.

Object Initialization

To create a new object, use the new statement to instantiate a class:

<?php class test { function checkTest() { echo "Doing Test."; } } $bar = new test; $bar->checkTest();<br ?--> <?php
class test
{
function checkTest()
{
echo "Doing Test.";
}
}

$bar = new test;
$bar->checkTest();
?>

By far the easiest and correct way to instantiate an empty generic PHP object that you can then modify for whatever purpose you choose:

<?php
$Object = new stdClass();
?>

I had the most difficult time finding this, hopefully it will help You guys!

Read more OOP in PHP

I hope its helpful for every one, contact & ask any question on http://fb.com/smartwebcareindia