ExpertRating - Online Certification and Employment Testing ExpertRating - Online Certification and Employment Testing ExpertRating - Online Certification and Employment Testing

ExpertRating Home
ExpertRating Benefits
Recommend ExpertRating
Suggest More Tests
Privacy Policy
FAQ
Login
 
PHP Tutorial - Index
PHP Tutorial - Introduction to PHP
PHP Tutorial - Your First PHP Script
PHP Tutorial - Operators
PHP Tutorial - Conditional and Looping
PHP Tutorial - Functions
PHP Tutorial - Classes and Objects
PHP Tutorial - Working with Forms
PHP Tutorial - Advanced PHP Topics
PHP Tutorial - Advanced Class Concepts
     

 
Online Personal Trainer Certification
Personal Trainer Certification (Spanish)
Six Sigma Certification
Find a Personal Trainer
Project Management Certification
Six Sigma Certification
Six Sigma Black Belt Certification
Master Black Belt Certification
Six Sigma Green Belt Certification
Life Coach Certification
Lean Management Certification
Lean Six Sigma Certification
First Aid Certification
CPR Certification
Yoga Certification
Aerobics Certification
Pilates Certification
Business Analyst Certification
SEO Certification
Online Business Writing Course & Certification
Makeup Artist Certification
Baby Sitting Course & Certification
Time Management Certification
Health Club Management Certification
Medical Coding Certification
Medical Billing Certification
Business Analysis Course
Master Trainer Certification
Total Quality Management Certification
Kaizen Certification
Creative Writing Certification
Event Management Certification
Test Search by Job Title
Administrative Assistant Certification
Virtual Assistant Certification
  All Online Courses
   

   
   
   
   
   
   

   
  Home > Courses, Tutorials & eBooks > PHP Tutorial >Advanced Class Concepts
 

PHP Tutorial - Advanced Class Concepts

 
Page 1 of 3
 

This chapter will be focusing on some advanced class concepts, which will empower the reader to use classes in a better way and to create dynamic web pages.

Constructor

In PHP 5 developers can declare constructor methods for classes. In this, those classes which have a constructor method call this method for each newly created object. So, it is suitable for any initialization that the object may need before it is used. In this the parent constructors are not called implicitly if the child class defines a constructor.

Example

<?php

class ParentClass {

function __construct() {

print "In ParentClass constructor\n";

}

}

class ChildClass extends ParentClass {

function __construct() {

parent::__construct();

print "In ChildClass constructor\n";

}

}

$obj = new ParentClass();

$obj = new ChildClass();

?>

 

 

Destructors

The destructor concept is introduced in PHP 5. This destructor concept is similar to other object oriented languages. In this the destructor will be called as soon as all references to a particular object have been removed or when the object has been explicitly destroyed.

Example

<?php

class MyClass {

function __construct() {

print "In constructor\n";

$this->name = "MyClass";

}

function __destruct() {

print "Destroying " . $this->name . "\n";

}

}

$obj = new MyClass();

?>

 

Patterns

Patterns are ways to describe best practices and good designs. The patterns show a flexible solution to common programming problems.

Factory Pattern

In the factory pattern the objects are instantiated at runtime. It is called a factory pattern since it is responsible for manufacturing an object. A parameterized factory receives the name of the class to instantiate as argument.

Example

<?php

class Welcome

{

// The parameterized factory method

public static function factory($type)

{

if (include_once 'Drivers/' . $type . '.php') {

$classname = 'Driver_' . $type;

return new $classname;

} else {

throw new Exception ('Driver not found');

}

}

}

?>

 

 

         

 

 
     
Home  |  About Us  |  Privacy Policy  |  Site Map  |  FAQs  |  Contact Us
 
© ExpertRating 2015. All Rights Reserved.