little PHP OOP help

Open discussion about any topic, as long as you abide by the rules of course!
Post Reply
dmmh
Posts: 2501
Joined: Thu Jan 04, 2001 8:00 am

little PHP OOP help

Post by dmmh »

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?
User avatar
Foo
Posts: 13840
Joined: Thu Aug 03, 2000 7:00 am
Location: New Zealand

Re: little PHP OOP help

Post by Foo »

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.
^misantropia^
Posts: 4022
Joined: Sat Mar 12, 2005 6:24 pm

Re: little PHP OOP help

Post by ^misantropia^ »

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
^misantropia^
Posts: 4022
Joined: Sat Mar 12, 2005 6:24 pm

Re: little PHP OOP help

Post by ^misantropia^ »

dmmh wrote:How can I do it?

Code: Select all

<?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.
dmmh
Posts: 2501
Joined: Thu Jan 04, 2001 8:00 am

Re: little PHP OOP help

Post by dmmh »

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: Select all

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: Select all

	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
dmmh
Posts: 2501
Joined: Thu Jan 04, 2001 8:00 am

Re: little PHP OOP help

Post by dmmh »

^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 Fri Sep 14, 2007 12:34 pm, edited 1 time in total.
dmmh
Posts: 2501
Joined: Thu Jan 04, 2001 8:00 am

Re: little PHP OOP help

Post by dmmh »

not quite what I meant though. Can't it be done 'my way'?
^misantropia^
Posts: 4022
Joined: Sat Mar 12, 2005 6:24 pm

Re: little PHP OOP help

Post by ^misantropia^ »

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?
dmmh
Posts: 2501
Joined: Thu Jan 04, 2001 8:00 am

Re: little PHP OOP help

Post by dmmh »

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!
dmmh
Posts: 2501
Joined: Thu Jan 04, 2001 8:00 am

Re: little PHP OOP help

Post by dmmh »

OOP is fun! hellofawork, but fun :D
Post Reply