Sharp Tutorial
Constructor in php

Python

Java

C

C++

HTML/CSS

Java Script

PHP

SQL

C Programs

PHP Introduction

  • PHP home
  • PHP introduction
  • How to install and run
  • Syntax and Rule in PHP
  • Data type and operator
  • If else in php
  • Loop in php
  • Array in php
  • Sring in php
  • Function in php
  • Include in php

Working with Forms

  • Get and Post
  • Get Form input into PHP
  • Form Validation

PHP with MySQl

  • Connect PHP & Mysql
  • Insert using PHP Mysql
  • Select using PHP MySql
  • Delete Update using PHP Mysql
  • Login Page Example in PHP

Advanced PHP

  • Session in PHP
  • Cookies in PHP
  • File Handling in PHP
  • File Upload/Download PH

Object Oriented PHP

  • Class Obejct in PHP
  • Constructor in PHP
  • Destructor in PHP
  • Inheritance in PHP

Constructor in php

PHP constructor used to initialize declared as function  __construct(). Constructor no need to call but automatically called when the object of the class is being created.

Constructor may be default constructor and paraterized constructor

Default Constructor in PHP

 <?php
class class1
{
function __construct() //default constructor
{
echo "Constructor";
}
}
$c1=new class1();
?>

 

In the above program constructor is being called automatically and output is : constructor

Parameterized Constructor in PHP

<?php
  class Employee
   { 
    public $empId=0;
    public $empName=0;
    
     function __construct($eid,$ename) //parameterized constructor
    {
           $this->empId=$eid;
	    $this->empName=$ename;
    }
    function printEmpDetails() //default function with no arguement.
    {
     echo "Emp Id= this->empId";
     echo "EmpName= this->empName"; 
     }
   }   // end of class
   $e1= new Employee(102,'john');
   $e1->printEmpDetails();
  ?>

 

Above program uses the paramterized constructor and gives the output as following.

Enquiry about Course

Ask your Question

Click Here

Sharp (2) Tutorials

Video/ C Introduction

Watch video in full size

Video tutorial

Follow by Email
Facebook
Facebook
fb-share-icon
YouTube
Copyright © Sharp Tutorial
Build with WordPress
Wordpress Social Share Plugin powered by Ultimatelysocial
Sharp Tutorial