Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:68810 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 41776 invoked from network); 1 Sep 2013 08:05:07 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 1 Sep 2013 08:05:07 -0000 Authentication-Results: pb1.pair.com smtp.mail=nikita.ppv@gmail.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=nikita.ppv@gmail.com; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.214.178 as permitted sender) X-PHP-List-Original-Sender: nikita.ppv@gmail.com X-Host-Fingerprint: 209.85.214.178 mail-ob0-f178.google.com Received: from [209.85.214.178] ([209.85.214.178:41650] helo=mail-ob0-f178.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 79/6E-55332-235F2225 for ; Sun, 01 Sep 2013 04:05:06 -0400 Received: by mail-ob0-f178.google.com with SMTP id ef5so3413971obb.23 for ; Sun, 01 Sep 2013 01:05:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; bh=9Dr8slKAFxBZPCvpdn/NEl0Zp0qg2wE9xN77SwRZRdk=; b=f3Zapk1xB8mxumlSE6nKVTAQqMx5qc2/1C1pSrzXN9VMvK60pnrd42pFudSZ7Hv9GO /Pdy8jzTivQlayjzxuz2e+SrZ5CkgNf41iy93gsyqAL3P3OkyJ2BG2DEEzEmhOcje0x/ CKFbwOki6Uf5a1n4QvoB+xtopvoC3lSL7BBI0m4iQ5+7eTo8Qcrvac0E85GcoHHl2xpu vgZaB8d3RmCLf5qxMubBD0vxdchXbiCU8CgTXr4/HMIK6LeRUemMAaqqw9fw8iyqh9OP 3Kez9VpfFx3CeClbJyXEUBmUtildv48sZpsW0HtSLzBWcUnWAmFywdYgKimlyHorJhWY jdfg== MIME-Version: 1.0 X-Received: by 10.182.186.97 with SMTP id fj1mr12293950obc.53.1378022703346; Sun, 01 Sep 2013 01:05:03 -0700 (PDT) Received: by 10.182.98.8 with HTTP; Sun, 1 Sep 2013 01:05:03 -0700 (PDT) In-Reply-To: <7AF31CC1D1554454AC95AE758D23E92E@gmail.com> References: <5220262A.6040702@sugarcrm.com> <5220437A.7050008@sugarcrm.com> <5220D212.3010101@sugarcrm.com> <61FCD6C4A31248078FEAD2BA73D7CD44@gmail.com> <7AF31CC1D1554454AC95AE758D23E92E@gmail.com> Date: Sun, 1 Sep 2013 10:05:03 +0200 Message-ID: To: Vartolomei Nicolae Cc: Sebastian Krebs , Stas Malyshev , Sara Golemon , Anthony Ferrara , "internals@lists.php.net" Content-Type: multipart/alternative; boundary=089e01493832b9c51904e54de905 Subject: Re: Function autoloading From: nikita.ppv@gmail.com (Nikita Popov) --089e01493832b9c51904e54de905 Content-Type: text/plain; charset=ISO-8859-1 On Sat, Aug 31, 2013 at 11:57 PM, Vartolomei Nicolae wrote: > On Saturday, August 31, 2013 at 9:03 PM, Sebastian Krebs wrote: > > > I already _have_ create files for functions of a namespace... Closed > source. > Can we take a look at them as an example? Maybe we can give you some advice > how to refactor this code :) > > So you will create files for constants? One file with a single line defining a constant? It seems like many people in this thread can't even imagine that there are reasonable schemes for function autoloading. So let me give you a practical autoloading scheme as an example. I have a library (https://github.com/nikic/iter) - it's not important what it does, just that it's function based -, which uses the namespaces iter, iter\fn and iter\rewindable. All functions (and classes!) belonging to the respective namespaces are stored in iter.php, iter.fn.php and iter.rewindable.php. [*] An autoloader for this scheme (according to current proposal) would look like this: php\autoload_register(function($name, $type) { if (0 !== strpos($name, 'iter\\')) return; // extract namespace portion of name $namespace = substr($name, 0, strrpos($name, '\\')); require __DIR__ . '/' . str_replace('\\', '.', $namespace) . '.php'; }, php\AUTOLOAD_FUNCTION | php\AUTOLOAD_CLASS); As you can see, this is a simple autoloader, verify similar to what you'd write for classes. Differences being a) only the namespace is used to locate the file, rather than the full name and b) I'm using . instead of /, but that's really irrelevant here. Furthermore note that the above autoloading function loads both functions and classes with the same scheme. As such I find it useful to specify something like FUNCTION|CLASS a the type. So, hopefully this clarifies that autoloading does not require "every function in its own file" or something like that. "Every namespace in its own file" works fine. This is somewhat akin to how Python modules are structured. Thanks, Nikita [*] This is not exactly true. There are functions located in the "wrong" file, but only due to the lack of autoloading. With autoloading it would be no problem to put all functions in the file belonging to their namespace. --089e01493832b9c51904e54de905--