Apr 02, 2009 14:07
Решил я для забавы пощупать механизмы деструкции в PHP и в процессе оного написал такую вот штуку. Далее приведен контент, который выводится в браузер. Он содержит собственно код ("самораспечатывание") и затем результат работы смысловой части. Кирилл, надеюсь, тебе будет весело, а других френдов у меня пока ещё нет. Эй, граждане, вы куда спрятались? :)
Auto-printable source
// Cast
class CreatureException extends Exception{
function __construct($message,Creature &$creature){
parent::__construct($message);
$creature->say($message);
}
}
abstract class Creature{
private $name,$pets,$master;
function __construct($name){
$this->name=$name;
$this->pets=array();
$this->say("Hello, world! :)");
}
public function getName(){
return $this->name;
}
abstract protected function pronounce($blah);
public function say($blah){
echo"
{$this->name} [".get_class($this)."] said: {$this->pronounce($blah)}
";
}
public function tell(Creature &$whom, $blah){
echo"
{$this->name} [".get_class($this)."] told {$whom->getName()}: {$this->pronounce($blah)}
";
}
public function tame(Creature &$pet){
$this->tell($pet,"Hey, come here!");
if($pet===$this) throw new CreatureException("Dammit, no one may speak to himself!",$this);
$this->pets[]=&$pet;
$pet->becomeAttachedTo($this);
$this->tell($pet,"From now on you are my pet.");
}
public function becomeAttachedTo(Creature &$master){
$this->master=&$master;
$this->tell($master,"I recognize you, my master.");
}
public function harakiri(){
$this->say("Harakiri!!!");
unset($this);
$this->say("I must be dead by now... it's a miracle!");
}
function __destruct(){
foreach($this->pets as $id=>$pet){
if(isset($pet)){
$this->tell($pet,"Would you die tonight for love? Would you join me in death?");
$pet->harakiri();
}
}
if(isset($this->master)){
$this->tell($this->master,"My master, you don't exist any more, but love is a miracle again and so I can still remember your name! %)");
}else{
$this->say("Without any master my life costs nothing to nobody...");
}
$this->say("Hasta la vista, baby! // +_+");
}
}
class Human extends Creature{
protected function pronounce($blah){
return $blah;
}
}
class Dog extends Creature{
protected function pronounce($blah){
return "I'm a poor speechless creature, but my eyes are kinda saying "{$blah}"";
}
}
class Cat extends Creature{
private static $fuckoffs=array("fuck off","screw yourself","get lost","crap!");
protected function pronounce($blah){
return substr($blah,0,5)."...oh, ".self::$fuckoffs[array_rand(self::$fuckoffs)]."...";
}
}
// Source
echo"Auto-printable source
";
echo strtr(file_get_contents(__FILE__),array(
'&'=>'&',
'<'=>'<',
'>'=>'>',
'"'=>'"',
"\n"=>'
',
"\t"=>' ',
));
echo"";
// Action
echo"Creature PHP drama
";
try{
$guy=new Human("John");
$dog=new Dog("Stinky");
$cat=new Cat("Fluffy");
$guy->tame($dog);
$guy->tame($cat);
$guy->tame($guy); // this may happen or may not - play with comments
$dog->tame($cat); // despite all the PHP miracles, this never happens IRL
echo"
...Success!...
";
}
catch(CreatureException $e){
echo "
...a bloody Exception caused the Apocalypse, the World crushes...
";
}
echo"
...if PHP was not so emo, it could be the last message, but...
";Creature PHP drama
John [Human] said: Hello, world! :)
Stinky [Dog] said: I'm a poor speechless creature, but my eyes are kinda saying "Hello, world! :)"
Fluffy [Cat] said: Hello...oh, crap!...
John [Human] told Stinky: Hey, come here!
Stinky [Dog] told John: I'm a poor speechless creature, but my eyes are kinda saying "I recognize you, my master."
John [Human] told Stinky: From now on you are my pet.
John [Human] told Fluffy: Hey, come here!
Fluffy [Cat] told John: I rec...oh, get lost...
John [Human] told Fluffy: From now on you are my pet.
John [Human] told John: Hey, come here!
John [Human] said: Dammit, no one may speak to himself!
...a bloody Exception caused the Apocalypse, the World crushes...
...if PHP was not so emo, it could be the last message, but...
John [Human] told Stinky: Would you die tonight for love? Would you join me in death?
Stinky [Dog] said: I'm a poor speechless creature, but my eyes are kinda saying "Harakiri!!!"
Stinky [Dog] said: I'm a poor speechless creature, but my eyes are kinda saying "I must be dead by now... it's a miracle!"
John [Human] told Fluffy: Would you die tonight for love? Would you join me in death?
Fluffy [Cat] said: Harak...oh, fuck off...
Fluffy [Cat] said: I mus...oh, screw yourself...
John [Human] said: Without any master my life costs nothing to nobody...
John [Human] said: Hasta la vista, baby! // +_+
Stinky [Dog] told John: I'm a poor speechless creature, but my eyes are kinda saying "My master, you don't exist any more, but love is a miracle again and so I can still remember your name! %)"
Stinky [Dog] said: I'm a poor speechless creature, but my eyes are kinda saying "Hasta la vista, baby! // +_+"
Fluffy [Cat] told John: My ma...oh, fuck off...
Fluffy [Cat] said: Hasta...oh, fuck off...
языки/PHP,
пощупал,
забавы