473,499 Members | 1,660 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Regarding object reference

4 New Member
Hello to members,

I am getting fatal error while running the following code. Any solutions to avoid the error.
The error is
PHP Fatal error: Call to a member function getD() on a non-object in ..... on line 24.

The objective is to call the getValue() function of of D type object. Your guidance is required. I am using PHP5+ and Apache 2.2 on windows XP.

The code is below:

Expand|Select|Wrap|Line Numbers
  1.  
  2. <?php
  3. class Z{
  4.     function Z(){
  5.  
  6.     }
  7. }
  8. class A extends Z{
  9.     function A(){
  10.         parent::Z();
  11.     }
  12.  
  13.     function &getB(){
  14.         $b = new B();
  15.         return $b;
  16.     }
  17. }
  18.  
  19. class B{
  20.     private $c;
  21.     public function B(){
  22.         $this->c = new C();
  23.     }
  24.  
  25.     public function &getD(){
  26.         return $this->c->getD();
  27.     }
  28.  
  29.     public function &getE(){
  30.         return $this->c->getE();
  31.     }
  32. }
  33.  
  34. class C{
  35.     private $f;
  36.     public function C(){
  37.         $this->f = array();
  38.     }
  39.  
  40.     private function setD(){
  41.         $this->f[0][0] = new D();
  42.     }
  43.  
  44.     private function setE(){
  45.         $this->f[0][1] = new E();
  46.     }
  47.  
  48.     public function &getD(){
  49.         return $this->f[0][0];
  50.     }
  51.  
  52.     public function &getE(){
  53.         return $this->f[0][1];
  54.     }
  55. }
  56.  
  57. class D{
  58.  
  59.     public function D(){
  60.     }
  61.  
  62.     public function getValue(){
  63.         return "Val";
  64.     }
  65. }
  66.  
  67. class E{
  68.     public function E(){
  69.     }
  70.  
  71.     public function getValue(){
  72.         return "Eval";
  73.     }
  74.  
  75. }
  76.  
  77. $a = new A();
  78. $b =& $a->getB();
  79. $d = $b->getD();
  80. echo $d->getValue()
  81. ?>
  82.  
regards,
Nov 1 '07 #1
3 1198
amitabhcy
4 New Member
Hello again

The code provided is a simulation of a project I am working on. The object structure is the same as in the case of the project.

Kindly look into the code and inform how to correct.

regards,
Nov 1 '07 #2
code green
1,726 Recognized Expert Top Contributor
This is very complex.
Is the error occuring in this class?
[PHP]class B{
private $c;
public function B(){
$this->c = new C();
}

public function &getD(){
return $this->c->getD();
}

public function &getE(){
return $this->c->getE();
}
}[/PHP] I don't think the C class has had a D object instantiated, so this line is illegal [PHP]return $this->c->getD();[/PHP] Well it has but within an array[PHP]private function setD(){
$this->f[0][0] = new D();[/PHP] So C can reference an array of D objects but you have not specified which of them in the B class.
Well that is my guess. this really is complex.
Nov 1 '07 #3
amitabhcy
4 New Member
Is the error occuring in this class?
The error occurs in the line number 79 of the original post. The objects are getting created. This is verified with the print_r function.

It seems some where a reference should have been placed. Dunno where.

regards
Nov 1 '07 #4

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

Similar topics

2
2101
by: Janus | last post by:
Hell This is a very basic question i know :O Formerly I developed in VisualBasic 6.0 and when a project is started you can select different project types. But what exactly is an ActiveX DLL? -...
7
2196
by: Mrinal Kamboj | last post by:
Hi , I am using OracleConnection object from Oracle ODP.net provider and following is the behaviour which i am finding bit strange : To start with my argument is based on followings facts : ...
2
1797
by: Renji Panicker | last post by:
I have a question regarding references. Consider the following code: <code> #include <iostream> class A { int _a1; int _a2; int _a3; };
16
2652
by: PeterAPIIT | last post by:
Hello all C++ expert programmer, i have wrote partial general allocator for my container. After reading standard C++ library and code guru article, i have several questions. 1. Why...
5
2626
by: coolguyaroundyou | last post by:
Consider the following codes: class abc { int i; public: abc() : i(0) {} void func() { .....some code....} };
0
7134
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
7014
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
7180
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
7229
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...
0
7395
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...
0
5485
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,...
1
4921
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
4609
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...
0
3103
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.