Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:65104 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 38356 invoked from network); 23 Jan 2013 03:20:43 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 23 Jan 2013 03:20:43 -0000 Authentication-Results: pb1.pair.com smtp.mail=ravazin@gmail.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=ravazin@gmail.com; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.212.51 as permitted sender) X-PHP-List-Original-Sender: ravazin@gmail.com X-Host-Fingerprint: 209.85.212.51 mail-vb0-f51.google.com Received: from [209.85.212.51] ([209.85.212.51:41402] helo=mail-vb0-f51.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 8D/C3-12923-B075FF05 for ; Tue, 22 Jan 2013 22:20:43 -0500 Received: by mail-vb0-f51.google.com with SMTP id fq11so6208877vbb.10 for ; Tue, 22 Jan 2013 19:20:40 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:x-received:date:message-id:subject:from:to :content-type; bh=GNOJVVryVz7iT2Xen/kfVKiPfSpsV+hAgnONo3m9fvI=; b=qlTfGfuDPbSIT/evurRWuvT2FAVkTsPklkC8HMlW2pFyaawij7h4DWWcCP2P9aE5be oG3GoxOUxXbyyu6uD5ORdvBmI0QqCR1OHnLOgUp9R7o5miR0K+9ugXT/nprihxkBta5N dRFUxziZYKM45V9gPZk4jliN7UGVDOurgiNhuRQHvpsiIX7Thu0o8Fgc6PiKEoiIj0QN OoJM/LIcN1ojkm78thhXjZRNd7HKwZjQx7379YRBve6eqBxWl6X+WsFItjdEDgkfGtQ1 b245VyA0DABHgFp3MlFoulFVqONWM+oSTQFkhaeQYVyGiQbd4r9DKQj5DPyF7bB1akK0 VKLw== MIME-Version: 1.0 X-Received: by 10.52.67.75 with SMTP id l11mr23093200vdt.29.1358911240102; Tue, 22 Jan 2013 19:20:40 -0800 (PST) Received: by 10.220.251.201 with HTTP; Tue, 22 Jan 2013 19:20:39 -0800 (PST) Date: Tue, 22 Jan 2013 22:20:39 -0500 Message-ID: To: internals@lists.php.net Content-Type: text/plain; charset=ISO-8859-1 Subject: RE: __init magic method From: ravazin@gmail.com (Dmitri Ravazin) First of all, let's not call this 'magic'. Static initializers aren't any more magical that object constructors. johannes@php.net wrote: > Adding an __init method as you wish will just add complexity and gain very little. > class A { > public function __init() { echo __CLASS__; } > } > class B extends D { > public function __init() { echo __CLASS__; } > } > class C { > public function __init() { echo __CLASS__; } > } > class D { > public function __init() { echo __CLASS__; } > } > class E { > public function __init() { echo __CLASS__; } > } > ?> > What should this print? ABCDE? I don't see any 'complexity' here. Correct answer is ADBCE, because subclasses might rely on something that happens in superclass'es initializer. So superclass initializer should definitely run first. > Now for a second game: What does the following script print? > > echo "A"; > class foo { static function __init() { echo "B"; } } > echo "C"; > ?> > ABC? ACB? BAC? I guess the later as the code next to A or C might depend > on the class already. And what if I put some echos in my autoload function and make foo implement A B C and D? What's that gonna print? And why should I care? As long as my initializer runs before this class is "used" (i.e. before I create an instance or call any static methods or variables), it makes no difference to me when __init() is called. Because anything __init() does should be contained to the class itself (or the parent), and should not be relying on, or changing anything in the global scope. Sure, you *can* put your echos there, just like you can put echos in your autoload functions, or your constructors... It doesn't sound like a good design choice to me, but it's your code. But hey, here's an idea. Let's drop class autoloading 'magic' as well, because it has all the same 'problems' that you've just described. It loads things at 'completely arbitrary' times, it's complicated, and it surely makes reading code 'too hard'. And the gains are 'very little', anyway. You can always load your classes manually with a bunch of require statements, right?