473,320 Members | 1,914 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,320 software developers and data experts.

syntax? how to declare a member of a class which is itself a class

how to declare a member of a class which is itself an object?

class cA
{
private $blah;
public function foo()
{
return $blah;
}
}

class cB
{
private cA $a; // I want this to be of type class A
}

Aug 23 '07 #1
3 1538
D_a_n_i_e_l wrote:
how to declare a member of a class which is itself an object?

class cA
{
private $blah;
public function foo()
{
return $blah;
}
}

class cB
{
private cA $a; // I want this to be of type class A
}
<code>
class cB
{
private $a;
public function __construct()
{
$this->a = new cA();
}
}
$cb = new cB();
print_r($cb);
</code>
Returns:
cB Object
(
[a:private] =>
[cA] =cA Object
(
[blah:private] =>
)

)
Joe
Aug 23 '07 #2
On Aug 23, 11:06 am, D_a_n_i_e_l <danwgr...@gmail.comwrote:
how to declare a member of a class which is itself an object?

class cA
{
private $blah;
public function foo()
{
return $blah;
}

}

class cB
{
private cA $a; // I want this to be of type class A

}
Class members don't have a type specified. They'll take on the type
of whatever you put in them -- so there's no need to declare it
explicitly (nor is there syntax to do so).

Aug 23 '07 #3
On Aug 23, 11:15 am, Joe Scylla <joe.scy...@gmail.comwrote:
D_a_n_i_e_l wrote:
how to declare a member of a class which is itself an object?
class cA
{
private $blah;
public function foo()
{
return $blah;
}
}
class cB
{
private cA $a; // I want this to be of type class A
}

<code>
class cB
{
private $a;
public function __construct()
{
$this->a = new cA();
}
}
$cb = new cB();
print_r($cb);
</code>

Returns:
cB Object
(
[a:private] =>
[cA] =cA Object
(
[blah:private] =>
)

)

Joe
Or, if you don't want to pass in the object:

class cB {

private $_cA; // object of class cA

public function setCA(cA $obj) {
$this->_cA = $obj;
}
}

placing the name of the class you are allowing to be passed to the
method ensures that it is the correct class. If not, a fatal error
occurs. You could, of course, do it with instanceof operator:
http://us3.php.net/manual/en/languag...ators.type.php

that way, you can gracefully handle exceptions.

Aug 23 '07 #4

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

Similar topics

8
by: Rich Grise | last post by:
I think I've finally found a tutorial that can get me started: http://www.zib.de/Visual/people/mueller/Course/Tutorial/tutorial.html and I've been lurking for awhile as well. What happened is,...
19
by: Nicolas Fleury | last post by:
Hi everyone, I would to know what do you think of this PEP. Any comment welcomed (even about English mistakes). PEP: XXX Title: Specialization Syntax Version: $Revision: 1.10 $...
4
by: Stephen Corey | last post by:
I've got 2 classes in 2 seperate header files, but within the same namespace. If I use a line like: // This code is inside Class2's header file Class1 *newitem = new Class1(param1, param2); ...
3
by: infinity | last post by:
Hi all, Is constructor a special member function? But I don't think it is either a member function or even a special member function although it has the syntax of a function. I think it confused...
16
by: danu | last post by:
I have a structure : typedef struct{ char magicNum; int width; int height; int maxGrey; int pixels; } ImageT;
4
by: Al_C | last post by:
OK, my K&R is about 10 years old now, I'm tring to understand the following: typedef struct { union { OPTICAL_REPORT optical; UINT8 battery_level; } shared; UINT8 combi;...
6
by: Howard | last post by:
Hi, I have a function in three unrelated but similar classes. The code in the member functions is identical for all three classes. What I want is to make a template which defines the function,...
0
by: D_a_n_i_e_l | last post by:
how to declare a member of a class which is itself an object? class cA { private $blah; public function foo() { return blah; } }
5
Banfa
by: Banfa | last post by:
So I have a little problem, I have a template class and that class contains a template function; now what I want to do is declare that function in the class (or indeed the entire class) as a friend...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.