schwobeseggl.de

ein Neigschmecktr in Baden

  • Artikel
  • Fotos
  • Impressum

PHP hates me – eine Set-Klasse

Posted at December 12, 2008

Kommentare gesperrt, weil dieses Posting Spambots magisch anzieht.

Mir ist aufgefallen, daß meine Klasse nicht abfängt wenn man zweimal dasselbe Objekt ins Set setzt. Deshalb ein kleines Update

Nils Langner von PHP hates me hat eine Aufgabe gestellt. Und hier meine spontane Lösung, vom Hirn ins Terminal :)

<?php
<div class="moz-text-plain" style="font-family: -moz-fixed; font-size: 13px;" lang="x-western">
<pre>class Foobar {
}

/**
 * @author Matthias Coy
 */
class Set {
    private $set = null;
    private $typ = null;
    private $className = null;

    /**
     * The method to add something to the set. The type of the set is defined by its first entry.
     * @param <type> $something
     * @public
     */
    function push($something) {
        if ($this->set == null) {
            $this->typ = gettype($something);
            $this->set = array();
            array_push($this->set, $something);
            if($this->typ == "object") {
                $class = new ReflectionClass($something);
                $this->className = $class->getName();
                echo "It's a class, with the name: '".$this->className."'\n";
            } else {
                echo "It's something else: '". $this->typ ."'";
            }
        } else {
            $myType = gettype($something);
            if($myType != $this->typ) {
                throw new Exception("Wrong Type, this set is a set of '".$this->typ."'");
            } else {
                if ($myType == "object") {
                    $class = new ReflectionClass($something);
                    if ($this->className != $class->getName()) {
                        throw new Exception("Right Type, but wrong class (is: '".$class->getName()."', should be: '".$this->className."')");
                    }
                    foreach($this->set as $entry) {
                        if ($something === $entry) {
                            throw new Exception('Right Type, but already in Set');
                        }
                    }
                }
                array_push($this->set, $something);
                echo "adding sth. (".$myType.")\n";
            }
        }
    }

    /**
     * Return the last inserted entry of the set.
     *
     * @return <type>
     * @public
     * @throws Exception if set is empty
     */
    function pop() {
        $returnValue = array_pop($this->set);
        if ($returnValue == null) {
            $this->set = null;
            $this->typ = null;
            $this->className = null;
            throw new Exception("Set is not yet defined, please add something first");
        }
        return $returnValue;
    }

    /**
     * Returns the amount of entries in the set
     *
     * @return <type> Entry of the set
     * @public
     */
    function count() {
        return count($this->set);
    }
}

$var1 = new Set();
$var2 = new Foobar();

$mySet = new Set();
$mySet->push($var1);
try {
    // Add something that is different to the types already in the set
    $mySet->push($var2); // throws execption
} catch (Exception $e) {
    print $e->getMessage()."\n";
}
try {
    // Add something which has the right type, but is already inside the set
    $mySet->push($var1); // throws execption
} catch (Exception $e) {
    print $e->getMessage()."\n";
}
while($mySet->count() > 0) {
    $mySet->pop();
}
$mySet->push($var2);
try {
    $mySet->push($var1);
} catch (Exception $e) {
    print $e->getMessage()."\n";
}
/* Output:
It's a class, with the name: 'Set'
Right Type, but wrong class (is: 'Foobar', should be: 'Set')
Right Type, but already in Set
It's a class, with the name: 'Foobar'
Right Type, but wrong class (is: 'Set', should be: 'Foobar')
 */

Ähnliche Artikel:

  1. Registry Pattern in Javascript
  2. Klassenvariablen
  3. __FUNCTION__ in Java
  4. Quassel – PHP_search
  5. Vererbung in Javascript anhand von Exceptions

PHP
  • Digg
  • Stumbleupon
  • Del.icio.us
  • Reddit
  • Technorati

If you enjoyed this post, please consider to leave a comment or subscribe to the feed and get future articles delivered to your feed reader.

Comments

2 Responses to “PHP hates me – eine Set-Klasse”
  • Nils says:
    12. December 2008 at 17:11

    Ich glaube man schreibt mich LANGNER. Aber ansonsten netter Ansatz ;) Ganz anders als meiner wird, aber sich interessant.

  • pennywise says:
    12. December 2008 at 17:14

    Ja, Tippfehler im Namen, tut mir leid :) Ist geändert. Jetzt muss ich nur noch zwei Wochen auf deine Auswertung warten :/

Search

Archives

  • July 2010
  • June 2010
  • May 2010
  • April 2010
  • March 2010
  • February 2010
  • January 2010
  • December 2009
  • November 2009
  • October 2009
  • September 2009
  • August 2009
  • July 2009
  • June 2009
  • May 2009
  • April 2009
  • March 2009
  • February 2009
  • January 2009
  • December 2008
  • November 2008
  • October 2008
  • September 2008
  • August 2008
  • July 2008
  • June 2008
  • May 2008
  • April 2008
  • March 2008
  • February 2008
  • January 2008
  • December 2007
  • November 2007
  • October 2007
  • March 2007
  • March 2006
  • November 2005
  • October 2005
  • September 2005

Categories

  • Arbeit
  • PC & Accessoires
    • Linux
  • Pfadfinder
  • Programmiersprachen
    • C#
    • Java
    • PHP
  • Sonstiges
  • Spass
  • Studium
    • Diplomarbeit
    • Praxissemester
    • USA
  • Uncategorized
  • Wordpress
  • Recent Posts

    • URLdump
    • Flagge zeigen
    • London
    • $N zufällig gewählte Dateien
    • Hamburg – Jetzt in echt und mit Bildern
  • Tags

    Addon Amerika Apple Auto bahn bild Computer dell Deutschland eisdiele Extension fahrrad Film firefox flash geburtstag google Indiana indianapolis Internet itunes Java Javascript junit kaputt karlsruhe Kino Kinofilm Laptop last.fm Linux Microsoft Mozilla PHP Praxissemester skype Spass system Thunderbird umzug USA windows wohnung Wordpress Zoll
  • Links

    • almightyfish - A new world order
    • DPSG Stamm Holzgerlingen - Die Homepage des Pfadfinderstamms der DPSG Holzgerlingen
    • eisdiele’s blog - Der Blog eines tollen Typs. Ich mein, er ist Pfadfinder, solche Leute sind per se einfach spitze …
    • Jule’s Blog - Ein Blog von der Jule :) in good ol’ Karlsruhe
    • Stephan’s Blog - Ein Komolitone

Powered by Wordpress | WP Premium theme by Premium Wordpress Themes
Copyright 2010 schwobeseggl.de. All rights reserved

  • Artikel
  • Fotos
  • Impressum