Electronics & Programming

develissimo

Open Source electronics development and programming

  • You are not logged in.
  • Root
  • » PHP
  • » [PHP-DEV] RFC: about class names as values [RSS Feed]

#1 Jan. 6, 2011 01:08:53

Johannes S.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

[PHP-DEV] RFC: about class names as values


On Wed, 2011-01-05 at 21:53 -0300, Martin Scotta wrote:
> $obj = newInstance( MyClass ); // notice. undefined constant MyClass

This describes the major change with your idea.

What happens if a constant MyClass exists?

Another question is something like this:

<?php
function factory($class) {
return new $class();
}

factory( SomeClass );
?>


To proper support this we'd have to make classes first class elements.
For making this consistent it would make sense to make functions first
class elements. And best drop the $ in front of variables and create a
new language. Everything else becomes a mess.

johannes



--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit:http://www.php.net/unsub.php

Offline

#2 Jan. 6, 2011 01:52:39

John L.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

[PHP-DEV] RFC: about class names as values


2011/1/5 Johannes Schlüter <johan***@*hp.net>

> On Wed, 2011-01-05 at 21:53 -0300, Martin Scotta wrote:
> > $obj = newInstance( MyClass ); // notice. undefined constant MyClass
>
> This describes the major change with your idea.
>
> What happens if a constant MyClass exists?
>
> Another question is something like this:
>
> <?php
> function factory($class) {
> return new $class();
> }
>
> factory( SomeClass );
> ?>
>
>
> To proper support this we'd have to make classes first class elements.
> For making this consistent it would make sense to make functions first
> class elements. And best drop the $ in front of variables and create a
> new language. Everything else becomes a mess.
>
> johannes
>
>
>
> --
> PHP Internals - PHP Runtime Development Mailing List
> To unsubscribe, visit:http://www.php.net/unsub.php>
>
I think he's actually proposing creating for each class the magic class
constant CLASS, so your example becomes:

<?php

function factory($class) {
return new $class();
}

factory( SomeClass::CLASS );

?>

This is actually doable without a magic class constant, but requires a
function or class constant to be declared in each class.

<?php
class SomeClass {
const CLASS = __NAMESPACE__ . '\' . __CLASS__;
static function getNameWithNSPath()
{
return __NAMESPACE__ . '\' . __CLASS__;
}
}


factory( SomeClass::getNameWithNSPath() );
?>

Perhaps this could be simplified with traits, if __NAMESPACE__ and __CLASS__
work in traits that way. In fact, that's an interesting question, what is
__NAMESPACE__ in a trait defined in one namespace, then used in a class in a
different namespace?

I think the point is that the factory function could exist without any
knowledge of the namespaces of the classes it would work on. Then, somewhere
else where the class has been aliased or is otherwise accessible without the
full namespace path, the developer wouldn't need to specify the full
namespace path to the factory, but could ask the class itself what it's full
namespace path was. I don't know that I agree with the idea, but I don't
think it requires making classes first class elements.

John

Offline

#3 Jan. 6, 2011 14:17:48

Martin S.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

[PHP-DEV] RFC: about class names as values


Yes, my intention was to only add a magic constant with the class, similar
to this

namespace Bar {
class Foo {
const KLASS = __CLASS__;
}
}

namespace Buzz {
use \Bar\Foo as BazFoo;

class Bar extends BazFoo {
const KLASS = __CLASS__;
}

$bar = new Bar;
$baz = new BazFoo;

var_dump( get_class($baz), BazFoo::KLASS);
var_dump( get_class($bar), Bar::KLASS );
}

This is 100% valid PHP 5.3.3 code, but that includes a lot of effort from
the developer. Someone miss to include the KLASS constant on a class and the
result is undefined.

If that PHP could add a magic constant --named CLASS or whatever you like--
to each class it will reduce the amount of class names hardcoded onto
strings, probably to zero.

The only issue that I found today is related to interfaces. I'm not sure if
they should include this sort of magic constant, but I would rather include
them just for consistency but, as I previously said, I'm not sure about this
one.

Martin Scotta


2011/1/5 John LeSueur <john.lesu***@*mail.com>

>
>
> 2011/1/5 Johannes Schlüter <johan***@*hp.net>
>
>> On Wed, 2011-01-05 at 21:53 -0300, Martin Scotta wrote:
>> > $obj = newInstance( MyClass ); // notice. undefined constant MyClass
>>
>> This describes the major change with your idea.
>>
>> What happens if a constant MyClass exists?
>>
>> Another question is something like this:
>>
>> <?php
>> function factory($class) {
>> return new $class();
>> }
>>
>> factory( SomeClass );
>> ?>
>>
>>
>> To proper support this we'd have to make classes first class elements.
>> For making this consistent it would make sense to make functions first
>> class elements. And best drop the $ in front of variables and create a
>> new language. Everything else becomes a mess.
>>
>> johannes
>>
>>
>>
>> --
>> PHP Internals - PHP Runtime Development Mailing List
>> To unsubscribe, visit:http://www.php.net/unsub.php>>
>>
> I think he's actually proposing creating for each class the magic class
> constant CLASS, so your example becomes:
>
>
> <?php
>
> function factory($class) {
> return new $class();
> }
>
> factory( SomeClass::CLASS );
>
> ?>
>
> This is actually doable without a magic class constant, but requires a
> function or class constant to be declared in each class.
>
> <?php
> class SomeClass {
> const CLASS = __NAMESPACE__ . '\' . __CLASS__;
> static function getNameWithNSPath()
> {
> return __NAMESPACE__ . '\' . __CLASS__;
> }
> }
>
>
> factory( SomeClass::getNameWithNSPath() );
> ?>
>
> Perhaps this could be simplified with traits, if __NAMESPACE__ and
> __CLASS__ work in traits that way. In fact, that's an interesting question,
> what is __NAMESPACE__ in a trait defined in one namespace, then used in a
> class in a different namespace?
>
> I think the point is that the factory function could exist without any
> knowledge of the namespaces of the classes it would work on. Then, somewhere
> else where the class has been aliased or is otherwise accessible without the
> full namespace path, the developer wouldn't need to specify the full
> namespace path to the factory, but could ask the class itself what it's full
> namespace path was. I don't know that I agree with the idea, but I don't
> think it requires making classes first class elements.
>
> John
>
>

Offline

#4 Jan. 7, 2011 09:34:11

Adi N.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

[PHP-DEV] RFC: about class names as values


Hi,

To proper support this we'd have to make classes first class elements.
> For making this consistent it would make sense to make functions first
> class elements. And best drop the $ in front of variables and create a
> new language. Everything else becomes a mess.
>

Closures are first-class citizens and it seems natural that they are so.
Perhaps we should rethink what consistency means for PHP. I don't see any
good reason why first-class objects could be evil, except that the $ could
indeed be a problem.

Regards,
Adrian



2011/1/6 Johannes Schlüter <johan***@*hp.net>

> On Wed, 2011-01-05 at 21:53 -0300, Martin Scotta wrote:
> > $obj = newInstance( MyClass ); // notice. undefined constant MyClass
>
> This describes the major change with your idea.
>
> What happens if a constant MyClass exists?
>
> Another question is something like this:
>
> <?php
> function factory($class) {
> return new $class();
> }
>
> factory( SomeClass );
> ?>
>
>
> To proper support this we'd have to make classes first class elements.
> For making this consistent it would make sense to make functions first
> class elements. And best drop the $ in front of variables and create a
> new language. Everything else becomes a mess.
>
> johannes
>
>
>
> --
> PHP Internals - PHP Runtime Development Mailing List
> To unsubscribe, visit:http://www.php.net/unsub.php>
>

Offline

#5 Jan. 7, 2011 15:03:52

Martin S.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

[PHP-DEV] RFC: about class names as values


Hi all,

I've wrote a simple snippet to show the importance of this feature.
This little piece of code, at a glance, tries to instantiate 2 same objects
by different ways, but fails because of a silently code bug.

-- start of code
<?php
namespace B\C {
class Foo {
function test() {
echo __CLASS__, PHP_EOL;
}
}
}

namespace A\B\C {
class Foo {
function test() {
echo __CLASS__, PHP_EOL;
}
}
}

namespace A {
$f1 = new B\C\Foo;
$f1->test();

$class = 'B\C\Foo';
$f2 = new $class;
$f2->test();
}
?>
--- end of code


This works this way because class names in strings are treated as fully
qualified class names.

Although this script was created to reproduce an programmer error it helps
to show the need for a safe way to pass class name as a values.

As a side note, '\A\B\C\Foo' refers to the same class that 'A\B\C\Foo', but
the string comparison will return false

Regards,
Martin Scotta

On Thu, Jan 6, 2011 at 11:47 AM, Michael Morris <dmgx.mich***@*mail.com>wrote:

> +1 to this. In a similar vein (and similar reasons) consider ::PARENT as
> well. Since parent is also a reserved word here too is a case where no code
> in existence will be in conflict. So
>
> class Foo {}
> class Bar extends Foo{}
>
> echo Bar::PARENT; // "Foo"
>
> 2011/1/6 Martin Scotta <martinsco***@*mail.com>
>
> Yes, my intention was to only add a magic constant with the class, similar
>> to this
>>
>> namespace Bar {
>> class Foo {
>> const KLASS = __CLASS__;
>> }
>> }
>>
>> namespace Buzz {
>> use \Bar\Foo as BazFoo;
>>
>> class Bar extends BazFoo {
>> const KLASS = __CLASS__;
>> }
>>
>> $bar = new Bar;
>> $baz = new BazFoo;
>>
>> var_dump( get_class($baz), BazFoo::KLASS);
>> var_dump( get_class($bar), Bar::KLASS );
>> }
>>
>> This is 100% valid PHP 5.3.3 code, but that includes a lot of effort from
>> the developer. Someone miss to include the KLASS constant on a class and
>> the
>> result is undefined.
>>
>> If that PHP could add a magic constant --named CLASS or whatever you
>> like--
>> to each class it will reduce the amount of class names hardcoded onto
>> strings, probably to zero.
>>
>> The only issue that I found today is related to interfaces. I'm not sure
>> if
>> they should include this sort of magic constant, but I would rather
>> include
>> them just for consistency but, as I previously said, I'm not sure about
>> this
>> one.
>>
>> Martin Scotta
>>
>>
>> 2011/1/5 John LeSueur <john.lesu***@*mail.com>
>>
>> >
>> >
>> > 2011/1/5 Johannes Schlüter <johan***@*hp.net>
>> >
>> >> On Wed, 2011-01-05 at 21:53 -0300, Martin Scotta wrote:
>> >> > $obj = newInstance( MyClass ); // notice. undefined constant MyClass
>> >>
>> >> This describes the major change with your idea.
>> >>
>> >> What happens if a constant MyClass exists?
>> >>
>> >> Another question is something like this:
>> >>
>> >> <?php
>> >> function factory($class) {
>> >> return new $class();
>> >> }
>> >>
>> >> factory( SomeClass );
>> >> ?>
>> >>
>> >>
>> >> To proper support this we'd have to make classes first class elements.
>> >> For making this consistent it would make sense to make functions first
>> >> class elements. And best drop the $ in front of variables and create a
>> >> new language. Everything else becomes a mess.
>> >>
>> >> johannes
>> >>
>> >>
>> >>
>> >> --
>> >> PHP Internals - PHP Runtime Development Mailing List
>> >> To unsubscribe, visit:http://www.php.net/unsub.php>> >>
>> >>
>> > I think he's actually proposing creating for each class the magic class
>> > constant CLASS, so your example becomes:
>> >
>> >
>> > <?php
>> >
>> > function factory($class) {
>> > return new $class();
>> > }
>> >
>> > factory( SomeClass::CLASS );
>> >
>> > ?>
>> >
>> > This is actually doable without a magic class constant, but requires a
>> > function or class constant to be declared in each class.
>> >
>> > <?php
>> > class SomeClass {
>> > const CLASS = __NAMESPACE__ . '\' . __CLASS__;
>> > static function getNameWithNSPath()
>> > {
>> > return __NAMESPACE__ . '\' . __CLASS__;
>> > }
>> > }
>> >
>> >
>> > factory( SomeClass::getNameWithNSPath() );
>> > ?>
>> >
>> > Perhaps this could be simplified with traits, if __NAMESPACE__ and
>> > __CLASS__ work in traits that way. In fact, that's an interesting
>> question,
>> > what is __NAMESPACE__ in a trait defined in one namespace, then used in
>> a
>> > class in a different namespace?
>> >
>> > I think the point is that the factory function could exist without any
>> > knowledge of the namespaces of the classes it would work on. Then,
>> somewhere
>> > else where the class has been aliased or is otherwise accessible without
>> the
>> > full namespace path, the developer wouldn't need to specify the full
>> > namespace path to the factory, but could ask the class itself what it's
>> full
>> > namespace path was. I don't know that I agree with the idea, but I don't
>> > think it requires making classes first class elements.
>> >
>> > John
>> >
>> >
>>
>
>

Offline

#6 Jan. 8, 2011 10:21:58

Ben S.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

[PHP-DEV] RFC: about class names as values


I think doing something like this is a good idea for classes and
interfaces.

Ben.



On 7/01/11 1:16 AM, Martin Scotta wrote:Yes, my intention was to only add a magic constant with the class, similar
to this

namespace Bar {
class Foo {
const KLASS = __CLASS__;
}
}

namespace Buzz {
use \Bar\Foo as BazFoo;

class Bar extends BazFoo {
const KLASS = __CLASS__;
}

$bar = new Bar;
$baz = new BazFoo;

var_dump( get_class($baz), BazFoo::KLASS);
var_dump( get_class($bar), Bar::KLASS );
}

This is 100% valid PHP 5.3.3 code, but that includes a lot of effort from
the developer. Someone miss to include the KLASS constant on a class and the
result is undefined.

If that PHP could add a magic constant --named CLASS or whatever you like--
to each class it will reduce the amount of class names hardcoded onto
strings, probably to zero.

The only issue that I found today is related to interfaces. I'm not sure if
they should include this sort of magic constant, but I would rather include
them just for consistency but, as I previously said, I'm not sure about this
one.

Martin Scotta


2011/1/5 John LeSueur<john.lesu***@*mail.com>2011/1/5 Johannes Schlüter<johan***@*hp.net>On Wed, 2011-01-05 at 21:53 -0300, Martin Scotta wrote:$obj = newInstance( MyClass ); // notice. undefined constant MyClassThis describes the major change with your idea.

What happens if a constant MyClass exists?

Another question is something like this:

<?php
function factory($class) {
return new $class();
}

factory( SomeClass );
?>


To proper support this we'd have to make classes first class elements.
For making this consistent it would make sense to make functions first
class elements. And best drop the $ in front of variables and create a
new language. Everything else becomes a mess.

johannes



--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit:http://www.php.net/unsub.phpI think he's actually proposing creating for each class the magic class
constant CLASS, so your example becomes:


<?php

function factory($class) {
return new $class();
}

factory( SomeClass::CLASS );

?>

This is actually doable without a magic class constant, but requires a
function or class constant to be declared in each class.

<?php
class SomeClass {
const CLASS = __NAMESPACE__ . '\' . __CLASS__;
static function getNameWithNSPath()
{
return __NAMESPACE__ . '\' . __CLASS__;
}
}


factory( SomeClass::getNameWithNSPath() );
?>

Perhaps this could be simplified with traits, if __NAMESPACE__ and
__CLASS__ work in traits that way. In fact, that's an interesting question,
what is __NAMESPACE__ in a trait defined in one namespace, then used in a
class in a different namespace?

I think the point is that the factory function could exist without any
knowledge of the namespaces of the classes it would work on. Then, somewhere
else where the class has been aliased or is otherwise accessible without the
full namespace path, the developer wouldn't need to specify the full
namespace path to the factory, but could ask the class itself what it's full
namespace path was. I don't know that I agree with the idea, but I don't
think it requires making classes first class elements.

John--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit:http://www.php.net/unsub.php

Offline

#7 Jan. 9, 2011 14:09:04

Martin V.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

[PHP-DEV] RFC: about class names as values


I think adding a magic constant or method for getting the class name would
be usefull in many scenarios, when referencing a specific class (e.g.
factories, configurations). It would also work well with namespaces and
refactoring tools e.g.:

$mock = $this->getMock('\\My\\Custom\\Namespace\\MyClass');

vs.

use My\Custom\Namespace\MyClass;
$mock = $this->getMock(MyClass::CLASS);

On 8 January 2011 11:21, Ben Schmidt <mail_ben_schm***@*ahoo.com.au> wrote:

> I think doing something like this is a good idea for classes and
> interfaces.
>
> Ben.
>
>
>
>
> On 7/01/11 1:16 AM, Martin Scotta wrote:
>
>> Yes, my intention was to only add a magic constant with the class, similar
>> to this
>>
>> namespace Bar {
>> class Foo {
>> const KLASS = __CLASS__;
>> }
>> }
>>
>> namespace Buzz {
>> use \Bar\Foo as BazFoo;
>>
>> class Bar extends BazFoo {
>> const KLASS = __CLASS__;
>> }
>>
>> $bar = new Bar;
>> $baz = new BazFoo;
>>
>> var_dump( get_class($baz), BazFoo::KLASS);
>> var_dump( get_class($bar), Bar::KLASS );
>> }
>>
>> This is 100% valid PHP 5.3.3 code, but that includes a lot of effort from
>> the developer. Someone miss to include the KLASS constant on a class and
>> the
>> result is undefined.
>>
>> If that PHP could add a magic constant --named CLASS or whatever you
>> like--
>> to each class it will reduce the amount of class names hardcoded onto
>> strings, probably to zero.
>>
>> The only issue that I found today is related to interfaces. I'm not sure
>> if
>> they should include this sort of magic constant, but I would rather
>> include
>> them just for consistency but, as I previously said, I'm not sure about
>> this
>> one.
>>
>> Martin Scotta
>>
>>
>> 2011/1/5 John LeSueur<john.lesu***@*mail.com>
>>
>>
>>>
>>> 2011/1/5 Johannes Schlüter<johan***@*hp.net>
>>>
>>> On Wed, 2011-01-05 at 21:53 -0300, Martin Scotta wrote:
>>>>
>>>>> $obj = newInstance( MyClass ); // notice. undefined constant MyClass
>>>>>
>>>>
>>>> This describes the major change with your idea.
>>>>
>>>> What happens if a constant MyClass exists?
>>>>
>>>> Another question is something like this:
>>>>
>>>> <?php
>>>> function factory($class) {
>>>> return new $class();
>>>> }
>>>>
>>>> factory( SomeClass );
>>>> ?>
>>>>
>>>>
>>>> To proper support this we'd have to make classes first class elements.
>>>> For making this consistent it would make sense to make functions first
>>>> class elements. And best drop the $ in front of variables and create a
>>>> new language. Everything else becomes a mess.
>>>>
>>>> johannes
>>>>
>>>>
>>>>
>>>> --
>>>> PHP Internals - PHP Runtime Development Mailing List
>>>> To unsubscribe, visit:http://www.php.net/unsub.php>>>>
>>>>
>>>> I think he's actually proposing creating for each class the magic class
>>> constant CLASS, so your example becomes:
>>>
>>>
>>> <?php
>>>
>>> function factory($class) {
>>> return new $class();
>>> }
>>>
>>> factory( SomeClass::CLASS );
>>>
>>> ?>
>>>
>>> This is actually doable without a magic class constant, but requires a
>>> function or class constant to be declared in each class.
>>>
>>> <?php
>>> class SomeClass {
>>> const CLASS = __NAMESPACE__ . '\' . __CLASS__;
>>> static function getNameWithNSPath()
>>> {
>>> return __NAMESPACE__ . '\' . __CLASS__;
>>> }
>>> }
>>>
>>>
>>> factory( SomeClass::getNameWithNSPath() );
>>> ?>
>>>
>>> Perhaps this could be simplified with traits, if __NAMESPACE__ and
>>> __CLASS__ work in traits that way. In fact, that's an interesting
>>> question,
>>> what is __NAMESPACE__ in a trait defined in one namespace, then used in a
>>> class in a different namespace?
>>>
>>> I think the point is that the factory function could exist without any
>>> knowledge of the namespaces of the classes it would work on. Then,
>>> somewhere
>>> else where the class has been aliased or is otherwise accessible without
>>> the
>>> full namespace path, the developer wouldn't need to specify the full
>>> namespace path to the factory, but could ask the class itself what it's
>>> full
>>> namespace path was. I don't know that I agree with the idea, but I don't
>>> think it requires making classes first class elements.
>>>
>>> John
>>>
>>>
>>>
>>
> --
> PHP Internals - PHP Runtime Development Mailing List
> To unsubscribe, visit:http://www.php.net/unsub.php>
>
>


--
Mvh
Martin Vium
Senior System Arkitekt
Sitevision

Offline

#8 Jan. 13, 2011 15:23:25

Martin S.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

[PHP-DEV] RFC: about class names as values


Hi all,

I don't know how the internal development process of PHP works.

First at all: was this feature approved?

if that is a "yes"...
is this feature going to be scheduled for some release?
Is it supposed that I will submit a patch?

Thanks you all,
Martin Scotta


On Sun, Jan 9, 2011 at 11:07 AM, Martin Vium <mar***@*itevision.dk> wrote:

> I think adding a magic constant or method for getting the class name would
> be usefull in many scenarios, when referencing a specific class (e.g.
> factories, configurations). It would also work well with namespaces and
> refactoring tools e.g.:
>
> $mock = $this->getMock('\\My\\Custom\\Namespace\\MyClass');
>
> vs.
>
> use My\Custom\Namespace\MyClass;
> $mock = $this->getMock(MyClass::CLASS);
>
> On 8 January 2011 11:21, Ben Schmidt <mail_ben_schm***@*ahoo.com.au>
> wrote:
>
> > I think doing something like this is a good idea for classes and
> > interfaces.
> >
> > Ben.
> >
> >
> >
> >
> > On 7/01/11 1:16 AM, Martin Scotta wrote:
> >
> >> Yes, my intention was to only add a magic constant with the class,
> similar
> >> to this
> >>
> >> namespace Bar {
> >> class Foo {
> >> const KLASS = __CLASS__;
> >> }
> >> }
> >>
> >> namespace Buzz {
> >> use \Bar\Foo as BazFoo;
> >>
> >> class Bar extends BazFoo {
> >> const KLASS = __CLASS__;
> >> }
> >>
> >> $bar = new Bar;
> >> $baz = new BazFoo;
> >>
> >> var_dump( get_class($baz), BazFoo::KLASS);
> >> var_dump( get_class($bar), Bar::KLASS );
> >> }
> >>
> >> This is 100% valid PHP 5.3.3 code, but that includes a lot of effort
> from
> >> the developer. Someone miss to include the KLASS constant on a class and
> >> the
> >> result is undefined.
> >>
> >> If that PHP could add a magic constant --named CLASS or whatever you
> >> like--
> >> to each class it will reduce the amount of class names hardcoded onto
> >> strings, probably to zero.
> >>
> >> The only issue that I found today is related to interfaces. I'm not sure
> >> if
> >> they should include this sort of magic constant, but I would rather
> >> include
> >> them just for consistency but, as I previously said, I'm not sure about
> >> this
> >> one.
> >>
> >> Martin Scotta
> >>
> >>
> >> 2011/1/5 John LeSueur<john.lesu***@*mail.com>
> >>
> >>
> >>>
> >>> 2011/1/5 Johannes Schlüter<johan***@*hp.net>
> >>>
> >>> On Wed, 2011-01-05 at 21:53 -0300, Martin Scotta wrote:
> >>>>
> >>>>> $obj = newInstance( MyClass ); // notice. undefined constant MyClass
> >>>>>
> >>>>
> >>>> This describes the major change with your idea.
> >>>>
> >>>> What happens if a constant MyClass exists?
> >>>>
> >>>> Another question is something like this:
> >>>>
> >>>> <?php
> >>>> function factory($class) {
> >>>> return new $class();
> >>>> }
> >>>>
> >>>> factory( SomeClass );
> >>>> ?>
> >>>>
> >>>>
> >>>> To proper support this we'd have to make classes first class elements.
> >>>> For making this consistent it would make sense to make functions first
> >>>> class elements. And best drop the $ in front of variables and create a
> >>>> new language. Everything else becomes a mess.
> >>>>
> >>>> johannes
> >>>>
> >>>>
> >>>>
> >>>> --
> >>>> PHP Internals - PHP Runtime Development Mailing List
> >>>> To unsubscribe, visit:http://www.php.net/unsub.php> >>>>
> >>>>
> >>>> I think he's actually proposing creating for each class the magic
> class
> >>> constant CLASS, so your example becomes:
> >>>
> >>>
> >>> <?php
> >>>
> >>> function factory($class) {
> >>> return new $class();
> >>> }
> >>>
> >>> factory( SomeClass::CLASS );
> >>>
> >>> ?>
> >>>
> >>> This is actually doable without a magic class constant, but requires a
> >>> function or class constant to be declared in each class.
> >>>
> >>> <?php
> >>> class SomeClass {
> >>> const CLASS = __NAMESPACE__ . '\' . __CLASS__;
> >>> static function getNameWithNSPath()
> >>> {
> >>> return __NAMESPACE__ . '\' . __CLASS__;
> >>> }
> >>> }
> >>>
> >>>
> >>> factory( SomeClass::getNameWithNSPath() );
> >>> ?>
> >>>
> >>> Perhaps this could be simplified with traits, if __NAMESPACE__ and
> >>> __CLASS__ work in traits that way. In fact, that's an interesting
> >>> question,
> >>> what is __NAMESPACE__ in a trait defined in one namespace, then used in
> a
> >>> class in a different namespace?
> >>>
> >>> I think the point is that the factory function could exist without any
> >>> knowledge of the namespaces of the classes it would work on. Then,
> >>> somewhere
> >>> else where the class has been aliased or is otherwise accessible
> without
> >>> the
> >>> full namespace path, the developer wouldn't need to specify the full
> >>> namespace path to the factory, but could ask the class itself what it's
> >>> full
> >>> namespace path was. I don't know that I agree with the idea, but I
> don't
> >>> think it requires making classes first class elements.
> >>>
> >>> John
> >>>
> >>>
> >>>
> >>
> > --
> > PHP Internals - PHP Runtime Development Mailing List
> > To unsubscribe, visit:http://www.php.net/unsub.php> >
> >
> >
>
>
> --
> Mvh
> Martin Vium
> Senior System Arkitekt
> Sitevision
>

Offline

#9 Jan. 13, 2011 18:32:22

Chad F.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

[PHP-DEV] RFC: about class names as values


Take this with a grain of salt, but:

On Thu, Jan 13, 2011 at 7:22 AM, Martin Scotta <martinsco***@*mail.com> wrote:
> Hi all,
>
> I don't know how the internal development process of PHP works.
>
> First at all: was this feature approved?

>From my experience, don't consider something "approved" until it's in
SVN, and even then maybe not (e.g. type hinting...), regardless of how
many people say "I think that sounds like a good idea".

>
> if that is a "yes"...
> is this feature going to be scheduled for some release?

See below.

> Is it supposed that I will submit a patch?

Mostly, the burden is on the person requesting a feature to submit a
patch, if for no other reason than that everyone is busy and that, as
the requester, you probably have the most interest in getting the
feature implemented.

Once a patch is out there, you're going to need to convince at least
one core dev to get behind your idea, since they are in the position
of getting it into SVN, and they have the best big picture view of
PHP. The core devs do often seem happy to help improve patches, if
they think they're worthwhile.

Then, perhaps it will get into SVN if a core dev likes it and no core
devs oppose it too much.
Then, perhaps it will be scheduled for a release.

>
> Thanks you all,
>  Martin Scotta
>

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit:http://www.php.net/unsub.php

Offline

  • Root
  • » PHP
  • » [PHP-DEV] RFC: about class names as values [RSS Feed]

Board footer

Moderator control

Enjoy the 24th of October
PoweredBy

The Forums are managed by develissimo stuff members, if you find any issues or misplaced content please help us to fix it. Thank you! Tell us via Contact Options
Leave a Message
Welcome to Develissimo Live Support