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 3 of 3
 

_tostring

The _tostring magic method allows a class to decide that how it will react when it is converted to string.

Example

<?php

// Declare a simple class

class Test

{

public $foo;

public function __construct($foo) {

$this->foo = $foo;

}

public function __toString() {

return $this->foo;

}

}

$class = new Test('Hello');

echo $class;

?>

 

Cloning

An object copy is created by using the clone keyword. An object's __clone() method cannot be called directly. When an object is cloned, PHP will perform a shallow copy of all of the object's properties. Any properties that are references to other variables, will remain references. If a __clone() method is defined, then the newly created object's __clone() method will be called, to allow any necessary properties that need to be changed.

Example

<?php

class SubObject

{

static $instances = 0;

public $instance;

public function __construct() {

$this->instance = ++self::$instances;

}

public function __clone() {

$this->instance = ++self::$instances;

}

}

class MyCloneable

{

public $object1;

public $object2;

function __clone()

{

// Force a copy of this->object, otherwise

// it will point to same object.

$this->object1 = clone($this->object1);

}

}

$obj = new MyCloneable();

$obj->object1 = new SubObject();

$obj->object2 = new SubObject();

$obj2 = clone $obj;

print("Original Object:\n");

print_r($obj);

print("Cloned Object:\n");

print_r($obj2);

?>

 

 

 

         

 

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