# RFC proposal - Syntactic sugar for cloning

- Thread: https://externals.io/message/61040
- Original archive: https://news-web.php.net/php.internals/61040
- Started by Amaury Bouchard on 2012-06-29T14:25:23+00:00
- Messages: 9
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #61040 — Amaury Bouchard — 2012-06-29T14:25:23+00:00

URL: https://externals.io/message/61040#61040

Hello everybody,

It's the first time I write on the internals mailing-list, so let me
introduce myself quickly. I'm a french and canadian CTO, working in Paris.
I lead some PHP projects (mainly the Temma framework and FineFS data
replication system).
I begin to learn PHP's internal engine, backed by Pierrick Charron.

I would like to do an RFC proposal (see below for the associated patch).
I was thinking about "what if PHP was a full-object language?". Like, for
example, how will we write the simplest code, assuming that objects are
handled using pointers since PHP 5.

Take a look at this code:
   $a = 3;
   $b = $a;
   $a++;

$b still contains the value 3.

Now, if we imagine that even the integer data type is managed in an object,
the same code will produce two pointers to the same object. Thus, $b will
have the value 4, as $a.
So, in this imaginary world, we would need to do a lot of object cloning. I
wondered how this could be less painful, and I thought about the Pascal
language's affectation operator (:=).

Then we would be able to write something like that:
   $a = 3;
   $b := $a;
   $c = $a;
   $a++;

$a equals 4, as $c. But $b equals 3.

Back in the real world, we are not cloning objects very often. But, like
many other syntactic sugars (as the short array syntax), I think it could
be handy in some circumstances.

There is a patch for this evolution, written by Pierrick.
Full source code: https://github.com/adoy/php-src/tree/amaury-clone
Code diff:
https://github.com/adoy/php-src/commit/5107c0355c50381c7e67230cdc9f563eb3936a15


I'm looking forward to your advices.

Cheers!

Amaury

---

## #61041 — johannes@schlueters.de — 2012-06-29T14:50:45+00:00

URL: https://externals.io/message/61040#61041 · In reply to: #61040

> Back in the real world, we are not cloning objects very often. But, like
> many other syntactic sugars (as the short array syntax), I think it could
> be handy in some circumstances.

Well, arrays are used all over the place. As you said: cloning is not.
Also this patch only covers one use case, the assignment of an copy. I
does not cover other cases, like cloning in an function call
   foo(clone $obj);
etc. So I see little need for that sugar, it will mostly hurt the teeth
(to stay in the picture)

johannes

---

## #61042 — Patrick ALLAERT — 2012-06-29T15:01:27+00:00

URL: https://externals.io/message/61040#61042 · In reply to: #61040

2012/6/29 Amaury Bouchard <amaury@amaury.net>:
> Hello everybody,
>
> It's the first time I write on the internals mailing-list, so let me
> introduce myself quickly. I'm a french and canadian CTO, working in Paris.
> I lead some PHP projects (mainly the Temma framework and FineFS data
> replication system).
> I begin to learn PHP's internal engine, backed by Pierrick Charron.
>
> I would like to do an RFC proposal (see below for the associated patch).
> I was thinking about "what if PHP was a full-object language?". Like, for
> example, how will we write the simplest code, assuming that objects are
> handled using pointers since PHP 5.
>
> Take a look at this code:
>   $a = 3;
>   $b = $a;
>   $a++;
>
> $b still contains the value 3.
>
> Now, if we imagine that even the integer data type is managed in an object,
> the same code will produce two pointers to the same object. Thus, $b will
> have the value 4, as $a.
> So, in this imaginary world, we would need to do a lot of object cloning. I
> wondered how this could be less painful, and I thought about the Pascal
> language's affectation operator (:=).
>
> Then we would be able to write something like that:
>   $a = 3;
>   $b := $a;
>   $c = $a;
>   $a++;
>
> $a equals 4, as $c. But $b equals 3.
>
> Back in the real world, we are not cloning objects very often. But, like
> many other syntactic sugars (as the short array syntax), I think it could
> be handy in some circumstances.
>
> There is a patch for this evolution, written by Pierrick.
> Full source code: https://github.com/adoy/php-src/tree/amaury-clone
> Code diff:
> https://github.com/adoy/php-src/commit/5107c0355c50381c7e67230cdc9f563eb3936a15
>
>
> I'm looking forward to your advices.
>
> Cheers!
>
> Amaury

Hi Amaury,

I have to agree with Johannes here, it looks like a benefit only in a
very few cases but introduces a lot of side changes like IDE syntax
parsing, to cite only one.
I really doubt the little benefit of such a change outperforms the
extra work involved.

Cheers,
Patrick

---

## #61045 — Pierrick Charron — 2012-06-29T17:06:24+00:00

URL: https://externals.io/message/61040#61045 · In reply to: #61042

I also agree with Johannes.

Pierrick



> 2012/6/29 Amaury Bouchard <amaury@amaury.net>:
> > […]
> Paris.
> > […]
> object,
> > […]
> cloning. I
> > […]
> https://github.com/adoy/php-src/commit/5107c0355c50381c7e67230cdc9f563eb3936a15
> > […]
>
> Hi Amaury,
>
> I have to agree with Johannes here, it looks like a benefit only in a
> very few cases but introduces a lot of side changes like IDE syntax
> parsing, to cite only one.
> I really doubt the little benefit of such a change outperforms the
> extra work involved.
>
> Cheers,
> Patrick

---

## #61047 — Amaury Bouchard — 2012-06-29T18:27:13+00:00

URL: https://externals.io/message/61040#61047 · In reply to: #61040

Yes, guys. I totally understand your point. As I said, I had this idea in a
dreamed context (good or bad dream? I don't know).
But still, I think it's intellectually interesting, even if it's not a good
concept for PHP.  :-)

Pierrick, I owe you a beer  ;-)
Le 29 juin 2012 19:06, "Pierrick Charron" <pierrick@webstart.fr> a écrit :

---

## #61049 — Pierrick Charron — 2012-06-29T18:47:58+00:00

URL: https://externals.io/message/61040#61049 · In reply to: #61047

No problem when you'll come in Montreal ! If you need any help don't
hesitate.

Pierrick



> Yes, guys. I totally understand your point. As I said, I had this idea in
> a dreamed context (good or bad dream? I don't know).
> But still, I think it's intellectually interesting, even if it's not a
> good concept for PHP.  :-)
>
> Pierrick, I owe you a beer  ;-)
> Le 29 juin 2012 19:06, "Pierrick Charron" <pierrick@webstart.fr> a écrit :

---

## #61051 — Paul Dragoonis — 2012-06-29T22:09:57+00:00

URL: https://externals.io/message/61040#61051 · In reply to: #61049

My input is that we should be focusing on features that PHP lacks, or
fixing bugs rather than adding more sugar syntax just for the sake of
adding it.

On Fri, Jun 29, 2012 at 7:47 PM, Pierrick Charron <pierrick@webstart.fr>wrote:

> No problem when you'll come in Montreal ! If you need any help don't
> hesitate.
>
> Pierrick
>
> 
>
> > […]
> écrit :

---

## #61054 — Michael Morris — 2012-06-30T14:39:09+00:00

URL: https://externals.io/message/61040#61054 · In reply to: #61051

Uhm...  &=


> My input is that we should be focusing on features that PHP lacks, or
> fixing bugs rather than adding more sugar syntax just for the sake of
> adding it.
>
> On Fri, Jun 29, 2012 at 7:47 PM, Pierrick Charron <pierrick@webstart.fr>wrote:
>
> > […]

---

## #61062 — Amaury Bouchard — 2012-07-01T11:37:50+00:00

URL: https://externals.io/message/61040#61062 · In reply to: #61054

@Michael : I'm not sure to understand what you mean.
The "&=" operator does a bitwise AND. But if it was an
assignment-by-reference operator, maybe an assignment-by-cloning operator
would have seemed a good idea?

Some people write code using "=&" like a dedicated operator. For example:
   $i = 3;
   $j =& $i;

(yes, I know they should write "$j = &$i;")

It would be an "inverted equivalent" if we were able to write :
   $i = new Int(3);
   $j := $i;

2012/6/30 Michael Morris <dmgx.michael@gmail.com>

> Uhm...  &=
>
> On Fri, Jun 29, 2012 at 6:09 PM, Paul Dragoonis <dragoonis@gmail.com>
> wrote:
> > […]
> idea in
> > […]
