PHP - Classes and Objects
Identify the error in this code:
class Fruit {
public $name;
function __construct() {
$this->name = 'Apple';
}
}
$f = Fruit();
echo $f->name;Identify the error in this code:
class Fruit {
public $name;
function __construct() {
$this->name = 'Apple';
}
}
$f = Fruit();
echo $f->name;$f = Fruit(); calls class like a function, which is invalid.new$f = new Fruit(); to create an object.new keyword when creating object. -> Option Cnew to instantiate objects [OK]new before class name to create objects [OK]15+ quiz questions · All difficulty levels · Free
Free Signup - Practice All Questions