PHP Traits
Page 1 of 114 Replies - 6142 Views - Last Post: 14 December 2011 - 09:34 AM
Replies To: PHP Traits
#2
Re: PHP Traits
Posted 05 August 2011 - 05:21 PM
#3
Re: PHP Traits
Posted 05 August 2011 - 05:51 PM
#4
Re: PHP Traits
Posted 05 August 2011 - 06:37 PM
#5
Re: PHP Traits
Posted 06 August 2011 - 12:42 AM
#6
Re: PHP Traits
Posted 06 August 2011 - 04:24 AM

POPULAR
They've come up with a whole system just to allow reusable code, which you would do in most other languages via multiple inheritance of other classes. What we're going to have now is basically instead of something that makes sense such as:
class Developer extends Human, Geek, Logical
We will have random mixed versions of it including:
class Developer {
use Human, Geek, Logical;
class Developer extends Human {
use Geek, Logical;
Then we'll get random mutated versions of our written traits used by developers who think they want something that they shouldn't:
trait Geek {
public function DoGeekyStuff() {
// Do some geeky stuff! This should be public so others can call it
}
}
class Developer {
use Human, Geek { DoGeekyStuff as private }, Logical; // Haha, screw you public API rubbishness!
}
-------------------------------------------------------
I think what annoys me most about this whole thing is that they must've spent so much time on this when they haven't fixed inherent problems with the language that have been there from the beginning.
Like the fact we can't do this:
$result = new Class()->doSomething();
But we can do this..
function newClass() {
return new Class();
}
$result = newClass()->doSomething();
It almost made me want to write a wrapper for creating new classes, such as:
function _($className) {
if (!class_exists($className))
return null;
return new $className;
}
and just use that anywhere I wanted a new class, but the _() function already exists. Perhaps I should call it a()...
Also, we can't do this:
function blabla() {
return array('test' => 'test!');
}
$test = blabla()['test'];
They would be soooooooooooo much more useful than anything Traits could ever provide to us. I really don't think the PHP dev team has a clue what people want.
Surely somebody shares my views on this..
[/rant]
This post has been edited by RudiVisser: 06 August 2011 - 04:33 AM
#7
Re: PHP Traits
Posted 06 August 2011 - 05:50 AM
#8
Re: PHP Traits
Posted 06 August 2011 - 10:01 AM
I find it sad that PHP is going this direction. RudiVisser brought up a lot of my concerns when I saw this feature and you then have to understand a new overriding hierarchy to use it... then of course we have to go explaining it to n00bs of why it is like this when there is no good reason.
Another PHP fail as far as I am concerned.
#9
Re: PHP Traits
Posted 10 August 2011 - 01:28 AM
RudiVisser, on 06 August 2011 - 05:24 AM, said:
[/rant]
Yeah I do. After getting my head around what traits do, they seem to do very little at all. Surely trait behaviour can be achieved by something like this:
class myTraitClass()
{
public function myTrait()
{
// Does something
}
}
class whyUseTraits()
{
protected $myTraits;
public function __construct()
{
$this->myTraits = new myTraitClass();
}
public function noNeedForTraits()
{
return $this->myTraits->myFunction();
}
}
I dunno, maybe I've got it wrong and I'm misinterpreting.
PS, good call on the $result = new Class()->doSomething(); I'm baffled as to the fact that you can't do that in PHP.
#10
Re: PHP Traits
Posted 10 August 2011 - 02:06 AM
e_i_pi, on 10 August 2011 - 09:28 AM, said:
RudiVisser, on 06 August 2011 - 05:24 AM, said:
[/rant]
Yeah I do. After getting my head around what traits do, they seem to do very little at all. Surely trait behaviour can be achieved by something like this:
..snip..
I dunno, maybe I've got it wrong and I'm misinterpreting.
Yes that of course would be possible, but traits are meaning to have functions actually "be a part" of the class they're used within.
So essentially traits are a fake version of multiple inheritance. If you had one class like yours, then it would be simple enough to do class whyUseTraits extends myTraitClass, but traits will "help" when you need to do something like class whyUseTraits extends myTraitClass, anotherTraitClass, yetAnotherTraitClass... It would be so much better if they just actually did that.
But like I said, PHP's development team is clearly thick, it's been going in the wrong direction for quite some time in my eyes.
And yes the whole instantiation/array thing is an absolute pain in the ass. Compared to the usefulness/time investment to get these implemented and I'm fairly certain that they would be much more well received by the community than traits are.
Don't they have some sort of focus group thing thinking if this would be a good idea?! I mean a focus group that contains non-PHP developers, of course.
#11
Re: PHP Traits
Posted 10 August 2011 - 02:18 AM
RudiVisser, on 06 August 2011 - 01:24 PM, said:
function blabla() {
return array('test' => 'test!');
}
$test = blabla()['test'];
They would be soooooooooooo much more useful than anything Traits could ever provide to us. I really don't think the PHP dev team has a clue what people want.
the Array Dereferencing feature of PHP 5.4 …
RudiVisser, on 06 August 2011 - 01:24 PM, said:
$result = new Class()->doSomething();
But we can do this..
function newClass() {
return new Class();
}
$result = newClass()->doSomething();
at least it has been in discussion (ref.)
#12
Re: PHP Traits
Posted 10 August 2011 - 02:30 AM
Dormilich, on 10 August 2011 - 10:18 AM, said:
I had completely missed array dereferencing!
Dormilich, on 10 August 2011 - 10:18 AM, said:
I saw something on the old mailing list that dated this back to around 2007, or perhaps even before PHP5's time.. Will be a miracle if they do it.
The patch files are so simple and it wouldn't really be too difficult to test for regressions, you'd hope.
#13
Re: PHP Traits
Posted 10 August 2011 - 02:55 AM
#14
Re: PHP Traits
Posted 25 October 2011 - 05:27 PM
#15
|
|

New Topic/Question
Reply



MultiQuote







|