[PHP-DEV] Re: Friend class/function

2015-11-05 Thread Stephen Coakley

On 11/01/2015 10:04 AM, georges wrote:

​Hi php internals,

I recently discovered ​the "friendship concept" in c++ and i really love
the concept.
And i wonder if the php internals would be interested to implements it in
the next 7.x major version.


​The concept is simple:

It would allow declared "friend"​class method/function to call a
private/protected member of an other class:


The syntax could be like this (for a single friendship):

private function extractDataFromCache() friend myFriendClass1
{

}


or like this for many friendships:

private function extractDataFromCache() friend
{myFriendClass1::method1, myFriendClass2::method1,
myFriendClass3::method1}
{

}


In this way extractDataFromCache() could be called directly by method1 in
myFriendClass1 and myFriendClass2 etc

Off course deep calling should not be allowed. (E.g a function inside
myFriendClass1 that will call extractDataFromCache() won't inherit the
friendship.

Regards,
Georges



As Rowan already wrote, package / namespace visibility is the way to go 
here instead. Personally I found adding "friendship" in C++ to be a poor 
decision, since it's usually a sign of a code smell (not *always* 
though). There are admittedly a couple of use cases, but keeping things 
inside a private namespace is probably a more elegant solution.


I'd support namespace visibility. +1

--
Stephen

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



Re: [PHP-DEV] Re: Friend class/function

2015-11-02 Thread Rowan Collins

Andrea Faulds wrote on 01/11/2015 19:35:
I recently wished PHP had this feature when trying to implement the 
Khronos Group Typed Arrays specification in PHP, which requires 
sharing certain data between unrelated (inheritance-wise) classes. I 
ended up having to expose a public method to get the data, but named 
in a way that was likely to deter use.


Would "package" (namespace-based) visibility have solved the problem in 
this case? That's something I've often wanted - "this is public within 
this Lib, but should never be used outside it".


Regards,
--
Rowan Collins
[IMSoP]

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



Re: [PHP-DEV] Re: Friend class/function

2015-11-02 Thread Leigh
On 2 November 2015 at 09:25, Rowan Collins  wrote:

> Andrea Faulds wrote on 01/11/2015 19:35:
>
>> I recently wished PHP had this feature when trying to implement the
>> Khronos Group Typed Arrays specification in PHP, which requires sharing
>> certain data between unrelated (inheritance-wise) classes. I ended up
>> having to expose a public method to get the data, but named in a way that
>> was likely to deter use.
>>
>
> Would "package" (namespace-based) visibility have solved the problem in
> this case? That's something I've often wanted - "this is public within this
> Lib, but should never be used outside it".
>
> This was worked on for 7, but had a fundamental problem blocking it - See
Guilherme's recent runtime execution scope mail.


Re: [PHP-DEV] Re: Friend class/function

2015-11-02 Thread Andrea Faulds

Hi Rowan,

Rowan Collins wrote:

Andrea Faulds wrote on 01/11/2015 19:35:

I recently wished PHP had this feature when trying to implement the
Khronos Group Typed Arrays specification in PHP, which requires
sharing certain data between unrelated (inheritance-wise) classes. I
ended up having to expose a public method to get the data, but named
in a way that was likely to deter use.


Would "package" (namespace-based) visibility have solved the problem in
this case? That's something I've often wanted - "this is public within
this Lib, but should never be used outside it".


Yep, that would have worked, and that sounds like a much cleaner 
solution. Package/namespace visibility would solve a bunch of other 
use-cases too.


Thanks!

--
Andrea Faulds
http://ajf.me/

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



[PHP-DEV] Re: Friend class/function

2015-11-01 Thread Andrea Faulds

Hi Georges,

georges wrote:

​Hi php internals,

I recently discovered ​the "friendship concept" in c++ and i really love
the concept.
And i wonder if the php internals would be interested to implements it in
the next 7.x major version.


I recently wished PHP had this feature when trying to implement the 
Khronos Group Typed Arrays specification in PHP, which requires sharing 
certain data between unrelated (inheritance-wise) classes. I ended up 
having to expose a public method to get the data, but named in a way 
that was likely to deter use.


I think friend classes might be useful, then. I'm not sure if they're 
necessarily something we should add to HPP, though. Are there that many 
use-cases?


Thanks.

--
Andrea Faulds
http://ajf.me/

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