473,396 Members | 2,158 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

PHP classes

107 100+
Hi,

If I have a class that defines another class, how do i access the sub one from the main code.

So I have something like:
<?
Expand|Select|Wrap|Line Numbers
  1. class A{
  2.    function A($foo){
  3.        $this->p = new B($foo);  
  4.  
  5.    }
  6.  
  7. }
  8.  
  9. class B{
  10.     function Z($myvar){
  11.       $this->ID= $myvar;
  12.  
  13.    }
  14.  
  15. }
  16.  
  17. ?>
But how would the main code go to access the ID property of B when an A has been defined... i.e. not like this:
Expand|Select|Wrap|Line Numbers
  1. $newvar = new A(666);
  2. echo "B=".$newvar->p->ID;
how should it look?

thanks

Andy
May 28 '08 #1
5 1230
pbmods
5,821 Expert 4TB
Heya, Andy.

The code you provided should work, since undeclared class members are assumed to be public in PHP.

A slightly more sane-looking approach might look something like this:

Expand|Select|Wrap|Line Numbers
  1. class A
  2. {
  3.   public $child;
  4.  
  5.   public function __construct( $subVal )
  6.   {
  7.     $this->child = new B($subVal);
  8.   }
  9. }
  10.  
  11. class B
  12. {
  13.   public $id;
  14.  
  15.   public function __construct( $id )
  16.   {
  17.     $this->id = $id;
  18.   }
  19. }
  20.  
  21. $newVar = new A(666);
  22. echo "B = {$newVar->child->id}";
  23.  
Alternatively, you might want A to be a wrapper for B. In this case, you might consider using magic methods to make the child class accessible:

Expand|Select|Wrap|Line Numbers
  1. class A
  2. {
  3.   protected $child;
  4.  
  5.   public function __construct( $subVal )
  6.   {
  7.     $this->child = new B($subVal);
  8.   }
  9.  
  10.   public function __get( $var )
  11.   {
  12.     if( isset($this->child->$var) )
  13.     {
  14.       return $this->child->$var;
  15.     }
  16.     else
  17.     {
  18.       return null;
  19.     }
  20.   }
  21. }
  22.  
  23. class B
  24. {
  25.   public $id;
  26.  
  27.   public function __construct( $id )
  28.   {
  29.     $this->id = $id;
  30.   }
  31. }
  32.  
  33. $newVar = new A(666);
  34. echo "B = {$newVar->id}";
  35.  
In the second example, PHP will look for A::$id, which doesn't exist, so it will instead call A::__get('id'), which will check to see if B::$id has been set.
May 29 '08 #2
theS70RM
107 100+
Thanks, that helps alot. I'll have a look at applying this to my code.
May 29 '08 #3
theS70RM
107 100+
a couple of questions..


Expand|Select|Wrap|Line Numbers
  1. class A
  2. {
  3.   public $id;
  4.  
  5.   public function __construct( $id )
  6.   {
  7.     $this->id = $id;
  8.   }
  9. }

how come function __construct(){} doesnt work for me,

how come I cant declare public functions or variables?

Cheers
May 29 '08 #4
pbmods
5,821 Expert 4TB
You are probably running PHP 4, which does not support these features.

Check out this document to see what features are available.
May 29 '08 #5
theS70RM
107 100+
oh ok,

Yea i am using php4 and cant upgrade at the mo.

cheers

Andy
May 30 '08 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Bob Rock | last post by:
Hello, in the last few days I've made my first few attempts at creating mixed C++ managed-unmanaged assemblies and looking aftwerwards with ILDASM at what is visible in those assemblies from a...
9
by: Jack | last post by:
Hello I have a library of calculationally intensive classes that is used both by a GUI based authoring application and by a simpler non-interactive rendering application. Both of these...
9
by: Aguilar, James | last post by:
I know that one can define an essentially unlimited number of classes in a file. And one can declare just as many in a header file. However, the question I have is, should I? Suppose that, to...
12
by: Langy | last post by:
Hello I'm fairly new to C++ but have programmed several other languages and found most of c++ fairly easy (so far!). I've come to a tutorial on classes, could someone please tell me why you...
4
by: john townsley | last post by:
do people prefer to design classes for the particular job or for a rangle of tasks they might encounter now and in the future. i am doing some simple win32 apps and picking classes is simple, but...
2
by: joye | last post by:
Hello, My question is how to use C# to call the existing libraries containing unmanaged C++ classes directly, but not use C# or managed C++ wrappers unmanaged C++ classes? Does anyone know how...
18
by: Edward Diener | last post by:
Is the packing alignment of __nogc classes stored as part of the assembly ? I think it must as the compiler, when referencing the assembly, could not know how the original data is packed otherwise....
6
by: ivan.leben | last post by:
I want to write a Mesh class using half-edges. This class uses three other classes: Vertex, HalfEdge and Face. These classes should be linked properly in the process of building up the mesh by...
0
by: ivan.leben | last post by:
I am writing this in a new thread to alert that I found a solution to the problem mentioned here: http://groups.google.com/group/comp.lang.c++/browse_thread/thread/7970afaa089fd5b8 and to avoid...
2
by: Amu | last post by:
i have a dll ( template class) ready which is written in VC++6. But presently i need to inherit its classes into my new C#.net project.so if there is some better solution with u then please give me...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.