473,772 Members | 2,522 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Friend classes

Now that PHP5 has proper scoping for class properties and methods, I've
been looking in vain for friend classes, or wondering if there's another
way to achieve this effect:

class A {

protected var $v1;

....

}

class AHelper {
....

function ....
{

....
$helper = new AHelper ( ... );

....
if ($helper->v1) ...

}

So A has a property which really is not supposed to be public, but
AHelper (which does not inherit from A) has a special relationship with
A and wants to be able to delve inside it.

Note that just providing a public accessor won't help: it's logically
the same as making the property public.

C++ manages this by having 'friend classes' - classes which are allowed
inside a class even though they are not derived from it.

I suppose I could write a public accessor method which takes its caller
as an argument and checks its type before it will go ahead and fetch the
value; but that's inelegant, heavy, and only takes effect at run time.

Has anybody got any better suggestions?

[I see somebody asked the same question two weeks ago on faqts, but did
not get an answer)

Colin
Oct 18 '05 #1
2 2227
Colin Fine said the following on 19/10/2005 00:19:
Now that PHP5 has proper scoping for class properties and methods, I've
been looking in vain for friend classes, or wondering if there's another
way to achieve this effect:


PHP OOP is *extremely* limited. I'm afraid you're going to have to live
with it... ;)
--
Oli
Oct 19 '05 #2
> PHP OOP is *extremely* limited. I'm afraid you're going to have to live
with it... ;)


Hey who are you calling limited >;)

--
Mike Willbanks
Zend Certified Engineer
http://www.digitalstruct.com
Oct 19 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
8600
by: Sebastian Faust | last post by:
Hi, is a construction like the following possible: template<class view_model> class template_clase { protected: template_clase() {} virtual ~template_clase() {}
15
6593
by: Samee Zahur | last post by:
Question: How do friend functions and static member functions differ in terms of functionality? I mean, neither necessarily needs an object of the class to be created before they are called and either has access only to static members of the class (ie. assuming no object of the class is in scope - neither by arguments recieved nor by local declarations). Any static member function like this: //accessing static member i static void...
7
9822
by: Jesper | last post by:
I need to grant a class access to protected fields of another class in the way its possible in C++ with the friend keyword. However I would like to keep the class protected towards other class within the same program/assembly. The two classes are not 'related' (inherited). How can I do this. I cant see how the keyword Internal can be used for this purpose.
4
4319
by: darrel | last post by:
In the context of VB.net, when/why would one use a 'friend class'. I'm finding lots of references to friend classes on google, but not a simle definition of what they actually are. Actually, if there is a nice overview of the different types of classes (public, private, shared, friend, etc.) and how they differ on a broad level, I'd certainly like to read it. I'm at the point where I'm a 'decent hack' and .net but I'm missing some...
2
1713
by: anongroupaccount | last post by:
I have a smaller class that essentially acts as a record for a larger class (stored in a vector inside of the larger class). In a sense, it's splitting functionality over two classes. I don't want any other classes to be able to use this smaller class, as it just wouldn't make sense. I figured that I could either declare the smaller class inline in my larger class (not keen on doing that as it makes a mess!) or give the smaller class...
8
1484
by: toton | last post by:
Hi, I have a parser interface , which is class IParser{ public: void readHeader(Document& doc)= 0; void readCC(CC& cc) = 0; }; Now, there are 3 kinds of parser which extends (implements) IParser, one for XML, ne for Config, & one for binary. They need to get the Document & CC class and fill the data, in the
3
1737
by: Neal | last post by:
Hi all Why do I get a compiler error with the following code. Friend Class SomeClass End Class Public Class SomePublicClass Protected Friend Function AMethod As SomeClass
5
2163
by: Amit Gupta | last post by:
How do I do it (either by friend or by any other hack). I have three class, I am just writing derivation below: class A{} class B{} class C : public class B{} class D: public class B ()
19
2723
by: subramanian100in | last post by:
Stroustrup, in his book TC++PL 3rd Edition, in page 16, under the section '1.8 Advice' has mentioned the following: Don't use global data(use members) Don't use global functions Don't use public data members. Don't use friends, except to avoid or . Consider the following scenarios: Scenario 1:
1
2530
by: Immortal_Nephi | last post by:
I want to show you an example how two classes can have relationship instead of an inheritance. The inheritance is not an option if you want to define two classes. The relationship between two classes can be friends when variables can be modified in both classes. As far as we discussed in an earlier post, but I try to make this code much clearer. The C++ Compiler generates an error if you try member function to be friend because member...
0
9621
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9454
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10264
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10106
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9914
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8937
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6716
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5484
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2851
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.