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();
Keywords
Related Questions
- What is the potential issue with the SQL query in the PHP code provided?
- When developing a login script in PHP, what are the best practices for handling user IDs?
- In what scenarios should developers consider reporting issues with time() discrepancies to their hosting provider or server administrator?