-
basic php5 oop question
How do I call the methods of another object from inside another object?
This doesn't seem right:
Code:
3 class errObj{
4
5 public function displayErr($msg){
6
7 echo $msg;
8 exit;
9
10 }
11
12 }
13
14 class doSomething{
15
16 public function doingIt(){
17
18 $msg="An error happened";
19 $err_obj=new errObj();
20 $err_obj->displayErr($msg);
21
22 }
23
24 }
25
26 $obj=new doSomething();
27 $obj->doingIt();
~
Thanks in advance for any insight.
-
Bearded (M|G)od
That syntax is alright. Is it throwing an error when you try that?
-
Originally Posted by MyFriendIsATaco
That syntax is alright. Is it throwing an error when you try that?
No error, just seems bad.
If everytime I want to use the errObj inside another object, I have to create it inside that other object, seems tedious.
-
Bearded (M|G)od
in your case, you may want to use a static function. Here would be the rewritten code:
Code:
class errObj{
public static function displayErr($msg){
echo $msg;
exit;
}
}
class doSomething{
public function doingIt(){
$msg="An error happened";
errObj::displayErr($msg);
}
}
$obj=new doSomething();
$obj->doingIt();
Last edited by MyFriendIsATaco; 08-12-2007 at 09:39 PM.
-
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|