Hi! Could we please enable some services and cookies to improve your experience and our website?

SQLize Online / PHPize Online  /  SQLtest Online

A A A
Share      Blog   Popular

SQLize.online is a free online SQL environment for quickly running, experimenting with and sharing code. You can run your SQL code on top of the most popular RDBMS including MySQL, MariaDB, SQLite, PostgreSQL, Oracle and Microsoft SQL Server.

Copy Format Clear
<?php abstract class Character { protected $name; protected $health = 100; private static $maxHealth = 100; public function __construct($name) { $this->name = $name; } // Abstract: all characters MUST define this abstract public function attack(); // Public method: can be called from anywhere public function heal() { $this->health += 10; if ($this->health > self::$maxHealth) { $this->health = self::$maxHealth; } echo "$this->name heals! Health is now $this->health\n"; } // Protected static: can be used in children, but not outside protected static function getMaxHealth() { return self::$maxHealth; } // Private method: only this class can use it private function secretPower() { return "Hidden Force!"; } public function revealSecret() { // This is okay: private method used from inside same class return $this->secretPower(); } } class Player extends Character { public function attack() { return "$this->name attacks with sword!"; } public function showMaxHealth() { // OK: calling protected static method from parent return "Max health is: " . self::getMaxHealth(); } } class Enemy extends Character { public function attack() { return "$this->name attacks with claws!"; } } // Test it out: $player = new Player("Hero"); echo $player->attack() . "\n"; // Hero attacks with sword! echo $player->showMaxHealth() . "\n"; // Max health is: 100 $player->heal(); // Hero heals! Health is now 110 -> capped at 100 echo $player->revealSecret() . "\n"; // Hidden Force! $enemy = new Enemy("Goblin"); echo $enemy->attack() . "\n"; // Goblin attacks with claws! // Invalid use cases (will error if uncommented): // echo $player->secretPower(); ❌ private // echo Character::getMaxHealth(); ❌ protected

Stuck with a problem? Got Error? Ask ChatGPT!

Copy Clear