Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:62191 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 46092 invoked from network); 15 Aug 2012 13:29:18 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 15 Aug 2012 13:29:18 -0000 Authentication-Results: pb1.pair.com smtp.mail=d.giedrius@gmail.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=d.giedrius@gmail.com; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.216.170 as permitted sender) X-PHP-List-Original-Sender: d.giedrius@gmail.com X-Host-Fingerprint: 209.85.216.170 mail-qc0-f170.google.com Received: from [209.85.216.170] ([209.85.216.170:54214] helo=mail-qc0-f170.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 30/C0-40941-C24AB205 for ; Wed, 15 Aug 2012 09:29:17 -0400 Received: by qcmt36 with SMTP id t36so1310572qcm.29 for ; Wed, 15 Aug 2012 06:29:14 -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=11q0KtbwugGoAYplAYU4ZW2IJk/FCz/eLevISuTi6rI=; b=rJJEaKCViCPdI8aJWVWEF8YvXiyhHJD09ujNEw00BIAUKZRSMWQ2UgI7uOJfFMjmMI ++qq4Kt41zzOpen+UIs0nfM/2V8rq6wPKSZF0NscqpDdt6nRUxYVravX8cvr07ot32vl zjio9Im5Fi7arK+Qrmhztdv7H6CzJNpaRVf+wE05Lvx9QDkVUDqMRlRcXkjdLNJYsDeW j/9IZzJ1kaf3f6styvlRYui5VCRaryIyKjYcclQ6KjozyLvQJZiPMkNI9SavS4gFAyr+ jo132wFkjzH1ppi/r4Vlt4KsCSVNHN3AHOCeAQyIuvD1pndTpv0f0us5gH1Hepr0ZGb+ 4SKg== MIME-Version: 1.0 Received: by 10.50.187.228 with SMTP id fv4mr16052728igc.37.1345037353791; Wed, 15 Aug 2012 06:29:13 -0700 (PDT) Received: by 10.64.136.45 with HTTP; Wed, 15 Aug 2012 06:29:13 -0700 (PDT) In-Reply-To: References: Date: Wed, 15 Aug 2012 16:29:13 +0300 Message-ID: To: Yahav Gindi Bar Cc: Paul Dragoonis , internals@lists.php.net Content-Type: text/plain; charset=ISO-8859-1 Subject: Re: [PHP-DEV] Proposal: use SomeClass::staticMethod From: d.giedrius@gmail.com (Giedrius Dubinskas) On Wed, Aug 15, 2012 at 2:19 PM, Yahav Gindi Bar wrote: > On Wed, Aug 15, 2012 at 2:09 PM, Paul Dragoonis wrote: >> >> Comments inline. >> >> On Wed, Aug 15, 2012 at 11:59 AM, Giedrius Dubinskas >> wrote: >> > Hello Internals! >> > >> > I'm just on and off luker here but thought I'll throw in an idea for a >> > feature I'd love to see in PHP: aliasing static methods. >> > >> > Syntax would look something like this: >> > >> > use Namespaced\SomeClass::staticMethod; >> > use Some\Foo::bar as fooBar; >> > >> > staticMethod(); // would call Namespaced\SomeClass::staticMethod() >> >> Then you're confusing the reader, they think you're calling a >> function, but you're actually calling a class method. Confusion++ Static method essentially is a function (with elevated access to containing class) so I don't see much of a problem here. >> >> > fooBar(); // would call Some\Foo::bar() >> >> What if a function called staticMethod() already exists, there'd be a >> bunch of confusion on referring to the right one. Aliased static method would be translated during compilation and no additional resolution rules would be required. If one would try to define a function with same name in same file as alias, that would result in fatal error just like with class aliases: use Foo::bar as fooBar(); function fooBar() {} // Fatal error: Cannot redeclare ... >> >> > >> > This would make code more readable, by removing the the noise of >> > repetition of class names. For use cases we can look at Java use cases >> > for "import static". >> >> When you find a function call, you'd have to scroll up to the top of >> the page to see if it's actually a method alias. In this case being >> explicit is a good thing, no scrolling, no confusion. As of now when we see ``fooBar()`` we already have no idea where that ``fooBar`` declaration is. It may be declared in same namespace in some other file, in global namespace in some other file or built in function. I don't think that explicit alias in same file adds much confusion to what we already have. >> > >> > Aliasing class constants like that would also be very nice. >> > >> > What does everyone think? >> > Would it be possible in PHP? >> > >> > -- >> > Giedrius Dubinskas >> >> Not that I don't welcome your suggestions, I encourage them, but for >> this paritcular one I vote -1 on it. >> >> Thanks. >> >> > >> > -- >> > PHP Internals - PHP Runtime Development Mailing List >> > To unsubscribe, visit: http://www.php.net/unsub.php >> > >> >> -- >> PHP Internals - PHP Runtime Development Mailing List >> To unsubscribe, visit: http://www.php.net/unsub.php >> > > Hi, > > To be honest, I'm not a fan of aliasing - and Paul supplied some of the > reasons that stands for me. > When one see an class / function declaration - I think that it'll make > confuse if he/she'll have to look if this is an alias or not. Besides of > that, there's still the issue of "overriding existing functions" rules which > can confuse the user. > > Put that aside, if you can bring some example of good practice it'll be > great :) I think a good example from top of my head would be PHPUnit testing framework. It has class PHPUnit_Framework_Assert that contains only static assertion methods like assertEquals(), assertTrue(), etc. Then it has class PHPUnit_Framework_TestCase that extends PHPUnit_Framework_Assert. AFAICT there is no other reason for this hierarchy except to allow shorter assertion syntax. Example from PHPUnit manual: require_once 'PHPUnit/Framework.php'; class MessageTest extends PHPUnit_Framework_TestCase { public function testMessage() { $this->assertTrue(FALSE, 'This is a custom message.'); } } What is more PHPUnit_Framework_TestCase also contains methods dedicated for mocking like once(), returnValue(), etc. Another example: class StubTest extends PHPUnit_Framework_TestCase { public function testReturnArgumentStub() { // Create a stub for the SomeClass class. $stub = $this->getMock('SomeClass'); // Configure the stub. $stub->expects($this->once()) ->method('doSomething') ->with($this->lessThen('something')) ->will($this->returnValue(true)); $this->assertTrue($stub->doSomething('foo')); $this->assertTrue($stub->doSomething('bar')); } } Note that PHPUnit manual promotes using $this despide the fact that these methods are ``public static``. I think assertions and mocking could be decoupled and would be more readable like this: use PHPUnit_Framework_Assert::assertTrue; use PHPUnit_Framework_Assert::lessThen; use PHPUnit_Framework_MockObject_Matcher::once; use PHPUnit_Framework_MockObject_Matcher::returnValue; class StubTest extends PHPUnit_Framework_TestCase { public function testReturnArgumentStub() { // Create a stub for the SomeClass class. $stub = $this->getMock('SomeClass'); // Configure the stub. $stub->expects(once()) ->method('doSomething') ->with(lessThen('something')) ->will(returnValue(true)); assertTrue($stub->doSomething('foo')); assertTrue($stub->doSomething('bar')); } } > Regards, > Yahav. -- Giedrius Dubinskas