schwobeseggl.de

ein Neigschmecktr in Baden

  • Artikel
  • Fotos
  • Impressum

PHP hates me – eine Set-Klasse

Posted at Dezember 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 sagt:
    12. Dezember 2008 um 17:11

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

  • pennywise sagt:
    12. Dezember 2008 um 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

  • August 2010
  • Juli 2010
  • Juni 2010
  • Mai 2010
  • April 2010
  • März 2010
  • Februar 2010
  • Januar 2010
  • Dezember 2009
  • November 2009
  • Oktober 2009
  • September 2009
  • August 2009
  • Juli 2009
  • Juni 2009
  • Mai 2009
  • April 2009
  • März 2009
  • Februar 2009
  • Januar 2009
  • Dezember 2008
  • November 2008
  • Oktober 2008
  • September 2008
  • August 2008
  • Juli 2008
  • Juni 2008
  • Mai 2008
  • April 2008
  • März 2008
  • Februar 2008
  • Januar 2008
  • Dezember 2007
  • November 2007
  • Oktober 2007
  • März 2007
  • März 2006
  • November 2005
  • Oktober 2005
  • September 2005

Categories

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

    • Schottland
    • Thunderbird Addons
    • URLdump
    • Flagge zeigen
    • London
  • Schlagwörter

    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