Skip to content

Instantly share code, notes, and snippets.

@VictorFursa
Last active October 20, 2016 21:10
Show Gist options
  • Save VictorFursa/13627951e5526cf056019afe560db4ee to your computer and use it in GitHub Desktop.
Save VictorFursa/13627951e5526cf056019afe560db4ee to your computer and use it in GitHub Desktop.
полиморф
<?php
abstract class Character
{
public $lvl= 1;
public $name;
public $power;
public $intellect;
abstract function updateStat();
public function __construct($name)
{
$this->name = $name;
}
function levelUp($level)
{
$this->lvl = $level;
return $this->updateStat();
}
public function getStat()
{
echo '<h1>' . $this->name . '</h1>'.'интелект' . $this->intellect . '<br>' . 'сила' . $this->power;
}
}
<?php
require_once "Character.php";
class Mage extends Character
{
public $intellect = 2;
public $power = 1;
function updateStat()
{
$this->power = $this->lvl -1 + ($this->power + 1);
$this->intellect = $this->lvl * 2 + ($this->intellect );
}
}
$mage = new Mage('Mage');
$mage->getStat();
$mage->levelUp(10);
$mage->getStat();
$mage->levelUp(1);
$mage->getStat();
<?php
require_once "Character.php";
class Warrior extends Character
{
public $intellect = 1;
public $power = 2;
function updateStat()
{
$this->intellect = $this->lvl -1 + ($this->intellect + 1);
$this->power = $this->lvl * 2 + ($this->power );
}
}
$warrior = new Warrior('Warrior');
$warrior->getStat();
$warrior->levelUp(10);
$warrior->getStat();
$warrior->levelUp(1);
$warrior->getStat();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment