Quake3World.com Forums
     General Discussion
        little PHP OOP help


Post new topicReply to topic
Login | Profile | | FAQ | Search | IRC




Previous topic | Next topic 
Topic Starter Topic: little PHP OOP help

Elite
Elite
Joined: 04 Jan 2001
Posts: 28249
PostPosted: 09-14-2007 03:49 AM           Profile Send private message  E-mail  Edit post Reply with quote


lo :)

I am delving into OOP PHP coding now, mainly used function based before.
Is there a way to call a method of class A inside a method from class B?
I dont want to inherit anything so it can be modified or whatever, I just wanne call a method inside another class.

How can I do it?
Or is it not possible?




Top
                 

Timed Out
Timed Out
Joined: 02 Aug 2000
Posts: 38067
PostPosted: 09-14-2007 03:56 AM           Profile   Send private message  E-mail  Edit post Reply with quote


You can't call a method from a class without first creating an instance of that class and giving it a name.

So if you make an instance of your class 'person' and call the instance 'tempperson' you would call the method in that class that you called 'setname' like this:

$tempperson->setname( $newname )

If you have a method inside a class which might be called without the class, it doesn't want to be IN the class.




Top
                 

Mentor
Mentor
Joined: 12 Mar 2005
Posts: 3958
PostPosted: 09-14-2007 04:04 AM           Profile Send private message  E-mail  Edit post Reply with quote


Foo wrote:
If you have a method inside a class which might be called without the class, it doesn't want to be IN the class.

Not necessarily. Check out, for instance, these design patterns:

http://en.wikipedia.org/wiki/Singleton_pattern
http://en.wikipedia.org/wiki/Factory_method_pattern




Top
                 

Mentor
Mentor
Joined: 12 Mar 2005
Posts: 3958
PostPosted: 09-14-2007 04:10 AM           Profile Send private message  E-mail  Edit post Reply with quote


dmmh wrote:
How can I do it?

Code:
<?php

class A {
        function process() {
                return $this->message . "\n";
        }
}

class B {
        function B($message) {
                $this->message = $message;
        }

        function process() {
                return A::process();
        }
}

$b = new B("Hello, world!");
print $b->process();

?>

As you see, the methods of class A are run in the context of the instance of class B.




Top
                 

Elite
Elite
Joined: 04 Jan 2001
Posts: 28249
PostPosted: 09-14-2007 04:30 AM           Profile Send private message  E-mail  Edit post Reply with quote


Foo wrote:
You can't call a method from a class without first creating an instance of that class and giving it a name.

So if you make an instance of your class 'person' and call the instance 'tempperson' you would call the method in that class that you called 'setname' like this:

$tempperson->setname( $newname )


ofcourse, I know this ;)

Foo wrote:
If you have a method inside a class which might be called without the class, it doesn't want to be IN the class.


how do you mean this?

this is what I am trying to do:

file: foldersCL.php (holds various shit used for stuff related to folders
Code:
class directory {
   private $origumask    = NULL;
   private $oldumask    = NULL;
   private $newDir    = NULL;

   public function makeDir($dir){
      $this->oldumask = umask(0);
      $this->newDir = @mkdir($dir, 0777);          //make new tmp directory
      $this->origumask = umask($this->oldumask);
   }
}?>


I am calling the method in another class inside the file imageCL, which holds my image resize and display stuff

the bit that doesn't work:
Code:
   public function setSaveFolder($folder){
      $this->saveFolder   = $folder;
      if (!empty($this->saveFolder) && !is_dir($this->saveFolder)){   //create the directory if it does not exist   
         $dir = new directory();
         $dir->makeDir($this->saveFolder);
      }
   }


it throws me a method undefined error for the makeDir() method

all classes are autloaded so no worries there




Top
                 

Elite
Elite
Joined: 04 Jan 2001
Posts: 28249
PostPosted: 09-14-2007 04:31 AM           Profile Send private message  E-mail  Edit post Reply with quote


^misantropia^ wrote:
Foo wrote:
If you have a method inside a class which might be called without the class, it doesn't want to be IN the class.

Not necessarily. Check out, for instance, these design patterns:

http://en.wikipedia.org/wiki/Singleton_pattern
http://en.wikipedia.org/wiki/Factory_method_pattern


thanks :)




Last edited by dmmh on 09-14-2007 04:34 AM, edited 1 time in total.

Top
                 

Elite
Elite
Joined: 04 Jan 2001
Posts: 28249
PostPosted: 09-14-2007 04:33 AM           Profile Send private message  E-mail  Edit post Reply with quote


not quite what I meant though. Can't it be done 'my way'?




Top
                 

Mentor
Mentor
Joined: 12 Mar 2005
Posts: 3958
PostPosted: 09-14-2007 04:58 AM           Profile Send private message  E-mail  Edit post Reply with quote


dmmh wrote:
all classes are autloaded so no worries there

Hmm, the code snippet itself appears to be syntactically valid so I expect it is a matter of some source file not being included. Could you post the entire source?




Top
                 

Elite
Elite
Joined: 04 Jan 2001
Posts: 28249
PostPosted: 09-14-2007 05:20 AM           Profile Send private message  E-mail  Edit post Reply with quote


mmm, I found it. the file name was different from the class name so it couldnt open the file in the __autoload function. Would have expected PHP to throw a error when it fails to load a require_once referenced file, but I guess I was assuming to much

edit: thanks btw, all!




Top
                 

Elite
Elite
Joined: 04 Jan 2001
Posts: 28249
PostPosted: 09-15-2007 09:41 AM           Profile Send private message  E-mail  Edit post Reply with quote


OOP is fun! hellofawork, but fun :D




Top
                 
Quake3World.com | Forum Index | General Discussion


Post new topic Reply to topic


cron
Quake3World.com
© ZeniMax. Zenimax, QUAKE III ARENA, Id Software and associated trademarks are trademarks of the ZeniMax group of companies. All rights reserved.
This is an unofficial fan website without any affiliation with or endorsement by ZeniMax.
All views and opinions expressed are those of the author.