A Flash Developer Resource Site

Results 1 to 5 of 5

Thread: basic php5 oop question

  1. #1
    Senior Member
    Join Date
    Mar 2000
    Posts
    584

    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.
    InFlash.com shows, games, greetings and more - you in?

  2. #2
    Bearded (M|G)od MyFriendIsATaco's Avatar
    Join Date
    Dec 2002
    Location
    Awesomeville.
    Posts
    3,045
    That syntax is alright. Is it throwing an error when you try that?

  3. #3
    Senior Member
    Join Date
    Mar 2000
    Posts
    584
    Quote 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.
    InFlash.com shows, games, greetings and more - you in?

  4. #4
    Bearded (M|G)od MyFriendIsATaco's Avatar
    Join Date
    Dec 2002
    Location
    Awesomeville.
    Posts
    3,045
    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.

  5. #5
    Senior Member
    Join Date
    Mar 2000
    Posts
    584
    Thanks!
    InFlash.com shows, games, greetings and more - you in?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center