Jump to content

Calling class methods


egorig

Recommended Posts

Okay, here's a question. We have a simple class

 

	class image extends MySQL
{
	var $connection;

	function __construct()
	{
		parent::__construct();
	}

	function generate_image ($filename) 
	{
                        //code here
	}
}

 

which method calling is better ?

1) image::generate_image('test.php');

2) $images = new image(); $images->generate_image('test.php');

 

Need to know more about pros and cons.

 

Very very thanks in advance.

Link to comment
https://forums.phpfreaks.com/topic/131082-calling-class-methods/
Share on other sites

i'm not completely positive about this next bit...:

 

i think that when you use class::method() to call a public method, php treats the method as any other regular function.

if you create a $foo = new class(), and then call $foo->method(), a new object of type class is created, initialized and stored in memory, and then the method is called to manipulate (usually) the properties of the object to achieve something.

 

so if this method depends on properties in the class (in other words, if the method calls a $this->property), you'd have to create an instance of the object, otherwise $this->property doesn't exist, and the method might not run properly.

however, if the method only uses data fed to it as arguments at the call, then using class::method() would use less memory than the other way (because no object is stored in memory).

 

check this out, since i'm really not sure about any of this. this was just educated guessing.

 

i do know that to use a class::method() call, the method has to be public.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.