What is the difference between cloning a class and extending a class in PHP?
Cloning a class in PHP involves creating a new instance of the class with the same properties and methods as the original class. On the other hand, extending a class in PHP involves creating a new class that inherits properties and methods from the original class, allowing for additional functionality to be added.
// Cloning a class
class OriginalClass {
public $property = 'value';
public function method() {
return 'method called';
}
}
$original = new OriginalClass();
$cloned = clone $original;
// Extending a class
class OriginalClass {
public $property = 'value';
public function method() {
return 'method called';
}
}
class ExtendedClass extends OriginalClass {
public function newMethod() {
return 'new method called';
}
}
$extended = new ExtendedClass();
Related questions
-
include require file organization
-
functions structure testability
-
mysql_query PHP compatibility