Hi All!
As stated in the Wiki RFC HowTo section I'm here to measure the reaction
about my proposal.
I would like to propose an RFC and then implement initializer list for
constructors in PHP.
Being a C++ developer I find this feature very useful and these are my
considerations:
- This will provide a very convenient way to avoid things like
public function __construct($name, $surname, $age)
{
$this->name = $name;
$this->surname = $surname;
$this->age = $age;
}
in favor of:
public function __construct($name, $surname, $age) : name(name),
surname(surname), age(age) {};
- Another possible use of this could be:
public function __construct() : host("127.0.0.1") {};
- It should not introduce BC (please correct me if I'm wrong)
- In C++ using initializer list is used over assignment also for
performance reasons but right now I can't say if this can be achieved in
PHP or not.
Initializer lists in C++:
http://en.cppreference.com/w/cpp/language/initializer_list
What do you think?
Thank you
Lorenzo Fontana
- In C++ using initializer list is used over assignment also for
performance reasons but right now I can't say if this can be achieved in
PHP or not.Initializer lists in C++:
http://en.cppreference.com/w/cpp/language/initializer_list
Mind that C++ doesn't do this to make code "nice" or "short" or whatever
in that area but primarily due to need of control. Not having
initializer lists means that object members in C++ have to be default
constructible. Given C++ code like this
struct C {
SomeType a;
C(const SomeType& a) {
this->a = a;
}
}
first calls the default constructor SomeType::SomeType() for a and then
the assignment constructor (SomeType::operator=(const SomeType&) or
similar)
Another reason why C++ needs this ist const-correctness. In the example
above it will fail if a is marked as const as the default constructor
will initialize and assignment on const objects isn't valid.
In PHP however the default constructor equivalent is setting the type to
NULL
which is (almost) for free while allocating the variable. Thus the
need isn't there.
You also write
public function __construct() : host("127.0.0.1") {};
as example. But that ignores PHP's way to define default values:
class Foo {
private $host = "127.0.0.1";
}
would be the PHP version to what you propose. At the same time I am
confussed, by using a literal string there I assume you want to allow
expressions there, but in other examples, like
public function __construct($name, $surname, $age) : name(name),
surname(surname), age(age) {};
you are not handling expressions (unless the constructor arguments are
being ignored and properties are being initialized using the global
constants name, surname and age, which I assume you didn't intend)
You are also not discussing inheritance. Can I do
class Base { /* .... */ }
class Extended extends Base {
public function __construct() : parent(42) {}
}
how does that relate to parent::__construct()?
In summary:
I consider this syntactic sugar which in my opinion doesn't increase
readability and in other languages solves an issue PHP doesn't have.
johannes
Hi,
the proposed syntax also clashes with the new return type hints. They don't
make sense on construct, but reusing the same syntax for two different
things should be avoided if possible.
Hack implements a related approach (without most of the c++ guarantees and
benefits as it is pure syntactic sugar). If php wants to have something
similar, it may be favorable to talk to them why they chose to implement it
the way they did.
http://docs.hhvm.com/manual/en/hack.constructorargumentpromotion.php
Regards,
Am 17.06.2015 14:25 schrieb "Johannes Schlüter" johannes@schlueters.de:
- In C++ using initializer list is used over assignment also for
performance reasons but right now I can't say if this can be achieved in
PHP or not.Initializer lists in C++:
http://en.cppreference.com/w/cpp/language/initializer_listMind that C++ doesn't do this to make code "nice" or "short" or whatever
in that area but primarily due to need of control. Not having
initializer lists means that object members in C++ have to be default
constructible. Given C++ code like thisstruct C {
SomeType a;
C(const SomeType& a) {
this->a = a;
}
}first calls the default constructor SomeType::SomeType() for a and then
the assignment constructor (SomeType::operator=(const SomeType&) or
similar)Another reason why C++ needs this ist const-correctness. In the example
above it will fail if a is marked as const as the default constructor
will initialize and assignment on const objects isn't valid.In PHP however the default constructor equivalent is setting the type to
NULL
which is (almost) for free while allocating the variable. Thus the
need isn't there.You also write
public function __construct() : host("127.0.0.1") {};
as example. But that ignores PHP's way to define default values:
class Foo {
private $host = "127.0.0.1";
}would be the PHP version to what you propose. At the same time I am
confussed, by using a literal string there I assume you want to allow
expressions there, but in other examples, likepublic function __construct($name, $surname, $age) : name(name),
surname(surname), age(age) {};you are not handling expressions (unless the constructor arguments are
being ignored and properties are being initialized using the global
constants name, surname and age, which I assume you didn't intend)You are also not discussing inheritance. Can I do
class Base { /* .... */ }
class Extended extends Base {
public function __construct() : parent(42) {}
}how does that relate to parent::__construct()?
In summary:
I consider this syntactic sugar which in my opinion doesn't increase
readability and in other languages solves an issue PHP doesn't have.johannes