note 89296 added to language.oop5

From: Date: Mon, 02 Mar 2009 15:40:25 +0000
Subject: note 89296 added to language.oop5
Groups: php.notes 
Request: Send a blank email to php-notes+get-151347@lists.php.net to get a copy of this message
There are a couple of tricks you can do with PHP's classes that programmers from C++, etc., will find very peculiar, but which can be useful. You can create instances of classes without knowing the class name in advance, when it's in a variable: <?php $type = 'cc'; $obj = new $type; // outputs "hi!" class cc { function __construct() { echo 'hi!'; } } ?> You can also conditionally define them by wrapping them in if/else blocks etc, like so: <?php if (expr) { class cc { // version 1 } } else { class cc { // version 2 } } ?> It makes up for PHP's lack of preprocessor directives. The caveat is that the if/else code body must have been executed before you can use the class, so you need to pay attention to the order of the code, and not use things before they're defined. ---- Server IP: 69.147.83.197 Probable Submitter: 89.100.60.13 ---- Manual Page -- http://www.php.net/manual/en/language.oop5.php Edit -- https://master.php.net/note/edit/89296 Del: integrated -- https://master.php.net/note/delete/89296/integrated Del: useless -- https://master.php.net/note/delete/89296/useless Del: bad code -- https://master.php.net/note/delete/89296/bad+code Del: spam -- https://master.php.net/note/delete/89296/spam Del: non-english -- https://master.php.net/note/delete/89296/non-english Del: in docs -- https://master.php.net/note/delete/89296/in+docs Del: other reasons-- https://master.php.net/note/delete/89296 Reject -- https://master.php.net/note/reject/89296 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#151347) next »