-->

Programming

PHP magic methods performance

by on 19 November 2010, under PHP, Programming

How is the performance of magic methods
(__call, __get, __set, …)?

Compared to direct method calls: bad. Here a short benchmark and the code I wrote for this benchmark (needs PHP 5.3):

Method Call: 3.01 seconds
Direct Access: 2.47 seconds
Magic Method (__call) 5.32 seconds
Magic Method (__get) 5.37 seconds
define('ITERATIONS', 2000000);
    function doBenchmark($name, Closure $closure){
        $start = microtime(true);
        for ($i=0; $i < ITERATIONS; ++$i) {
            $closure();
        }
        $stop = microtime(true);
        echo "Test $name: " . ($stop - $start) . " seconds";
    }

    class Test{
        public $data = array("view" => "view", "test2" => "test2", "test3" =>"test3");

        public function getView(){
            return $this->data['view'];
        }

        public function __call($name, $arg){
           return $name=="_getView" ? $this->data['view'] : false;
        }

        public function __get($key){
            return array_key_exists($key, $this->data) ? $this->data[$key] : null;
        }
    }

    $t = new Test();
    doBenchmark("Methode Call", function(){
        global $t;
        $v = $t->getView();
    });

    doBenchmark("Direct Access", function(){
        global $t;
        $v = $t->data['view'];
    });

    doBenchmark("Magic Methode (__call)", function(){
        global $t;
        $v = $t->_getView();
    });

    doBenchmark("Magic Methode (__get)", function(){
        global $t;
        $v = $t->view;
    });

So, you get the picure ;)

Don’t use magic methods as a replacement for a regular method if a direct access to a field or variable is possible. Magic methods are the sugar for dynamic created attributes (eg. for database objects) or if you don’t know the method/attribute while writing the code.

Use magic methods only rarely.

The only special magic method you should use often: __autoload ;-) It helps to load your needed classes on demand only when they are needed. If you use namespaces, here a short trick to load the name-mangled classes (the $path must of course be changed to your needs):


function __autoload($class) {
    $parts = explode('\\', $class);
    $class = end($parts);
    $path = __DIR__ . "/includes/"); //path for the includes

     require ($path . $class . ".php");
}

Happy coding ;-)

Leave a Comment more...

Flash Symbole mit Actionscript3 in Flex

by on 23 Februar 2009, under Flash & Flex, Programming

Der Unterschied

Nicht vergessen: Flash != Flex. Flex benutzt zwar Flash Elemente basiert aber auf eigenen Klassen. Wenn Flash und Flex zusammenspielen sollen, benötigen wir eine Schnittstelle.
Ein MovieClip ist ein Anzeigeobjekt in Flash und UIComponent ist ein Anzeigeobjekt in Flex. Wir können aber ein MovieClip in ein UIComponent “verpacken”, so wird das Flex UIComponent ein Container für unseren Flash MovieClip.
Hier nun beide Dateien, zuerst die MXML Datei:





Und die Actionscriptdatei mit der Klasse:


package {
	import mx.core.Application;
	import mx.core.UIComponent;
	import flash.display.MovieClip;

	/*****************************************************************
	 * File: FlexApp.as
	 *****************************************************************/

	public class FlexApp {
		private var _app:Application;

		public function FlexApp(app:Application) {
			_app=app;

			var btn:MovieClip=new BtnStop();
			var flexui:UIComponent=new UIComponent;
			flexui.addChild(btn);
			app.addChild(flexui);
		}

		static public function initApp():FlexApp {
			return new FlexApp(Application(Application.application));
		}

		public function get app():Application {
			return _app;
		}
	}
}

Wie es funktioniert

applicationComplete in der MXML Datei ist der Eventhandler der von der Anwendung aufgerufen wird. Hier setzen wir unseren Handler ein, der eine statische Funktion unser Klasse sein muss damit er auch später aufgerufen werden kann.

initApp() erzeugt unsere Klasse und übergibt das Objekt als Parameter an den Constructor.

Im Constructor erzeugen wir den MovieClip, basierend auf dem von Flash importierten Symbol “BtnStop” (dieses Symbol kommt von einer SWC Bibliothek die vorher mit Flash erzeugt wurde). Es wird dann ein Flex UIComponent erzeugt, wo wir unseren MovieClip als Child hinzufügen. Das UIComponent kann nun zur Flex-Anwendung hinzugefügt werden.

Leave a Comment :, more...

Suche

Benutze das Eingabefeld für die Suche:

Archive

Alle Einträge, chronologisch...

© 2009 by Frank Arnold | Impressum