Welcome! Log In Create A New Profile

Advanced

[PHP] insteadof considered harmful

Posted by Szczepan Hołyszewski 
Szczepan Hołyszewski
[PHP] insteadof considered harmful
April 09, 2012 01:20PM
I must admit with embarrassment that after months of googling and posting
questions to various forums I still fail to understand the purpose of the
"insteadof" keyword and the insteadof clause.

As I currently see it, the whole insteadof clause is completely redundant. In
a clause like this:

Foo::tweak insteadof Bar;

the "insteadof Bar" part does not specify any information that is not already
unambiguously specified by the "Foo::tweak" part. "Foo::tweak;" already
conveys the intention of using tweak from the trait Foo instead of any other
trait that has a member named tweak. What if we are using seven such traits?
Do we have to list them all after insteadof? Why do we have to explicitly
enumerate things that we DON'T want to use?

I would like to see a small code example where the insteadof clause provides
information that is BOTH necessary to make the program unambiguous AND cannot
be conveyed with the simple "Foo::tweak;" syntax. Absent such example, I
consider "insteadof" harmful because it does nothing and adds a maintenance
chore. It should be made optional and deprecated ASAP, and removed at some
point in the future.

Szczepan Hołyszewski

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
TR Shaw
Re: [PHP] insteadof considered harmful
April 09, 2012 04:00PM
On Apr 9, 2012, at 7:15 AM, Szczepan Hołyszewski wrote:

>
> I must admit with embarrassment that after months of googling and posting
> questions to various forums I still fail to understand the purpose of the
> "insteadof" keyword and the insteadof clause.
>
> As I currently see it, the whole insteadof clause is completely redundant. In
> a clause like this:
>
> Foo::tweak insteadof Bar;
>
> the "insteadof Bar" part does not specify any information that is not already
> unambiguously specified by the "Foo::tweak" part. "Foo::tweak;" already
> conveys the intention of using tweak from the trait Foo instead of any other
> trait that has a member named tweak. What if we are using seven such traits?
> Do we have to list them all after insteadof? Why do we have to explicitly
> enumerate things that we DON'T want to use?
>
> I would like to see a small code example where the insteadof clause provides
> information that is BOTH necessary to make the program unambiguous AND cannot
> be conveyed with the simple "Foo::tweak;" syntax. Absent such example, I
> consider "insteadof" harmful because it does nothing and adds a maintenance
> chore. It should be made optional and deprecated ASAP, and removed at some
> point in the future.

See http://us.php.net/manual/en/language.oop5.traits.php and scroll down to conflict resolution
to see simple example. It is used to resolve method naming conflicts when multiple traits are used to emulate multiple inheritance.

Tom


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Szczepan Hołyszewski
Re: [PHP] insteadof considered harmful
April 09, 2012 05:40PM
> See http://us.php.net/manual/en/language.oop5.traits.php and scroll down to
> conflict resolution to see simple example. It is used to resolve method
> naming conflicts when multiple traits are used to emulate multiple
> inheritance.

I have read the manual, and the examples there are precisely my examples of
insteadof's complete redundancy. Firstly, the manual says:

"To resolve naming conflicts between Traits used in the same class, the
insteadof operator needs to be used to chose exactly one of the conflicting
methods."

Why, why, why? Why not just name that "exactly one of the conflicting methods"
using a fully trait-qualified name? That would be completely unambiguous. It's
a simple task of choosing one from many, and such a choice can be
unambiguously encoded by just naming the one, without having to list the
remaining many. It is really that simple, no strings attached.

Here's the example from the manual:

trait A {
public function smallTalk() {
echo 'a';
}
public function bigTalk() {
echo 'A';
}
}

trait B {
public function smallTalk() {
echo 'b';
}
public function bigTalk() {
echo 'B';
}
}

class Talker {
use A, B {
B::smallTalk insteadof A;
A::bigTalk insteadof B;
}
}

class Aliased_Talker {
use A, B {
B::smallTalk insteadof A;
A::bigTalk insteadof B;
B::bigTalk as talk;
}
}

I've been analyzing these 28 lines of code for months, and I cannot for the
life of me understand why the same information couldn't be conveyed thus:

/* no changes in the traits */

class Talker {
use A, B {
B::smallTalk;
A::bigTalk;
}
}

class Aliased_Talker {
use A, B {
B::smallTalk;
A::bigTalk;
B::bigTalk as talk;
}
}

All information is there. The insteadof clauses add nothing to the
interpreter's knowledge of the programmer's intention. They are every last one
of all the hundred damn percent redundant.

But perhaps there IS a purpose, only the examples in the manual fail to
demonstrate it?

Szczepan Hołyszewski

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Sorry, only registered users may post in this forum.

Click here to login