Jump to content

Difference between ClassName::function() and $this->function()


Wuhtzu

Recommended Posts

Hey

 

As the topic says, what is the difference between

 

ClassName::function()

 

and

 

$this->function()

 

??

 

The only thing I can think of / has noticed is that the first can be used in case of inheritage and the second one can't.

 

Thanks for your input

Wuhtzu

Link to comment
Share on other sites

Thanks, matthewhaworth.

 

When would you use a function from a class without instantiate the class?

 

When the function doesn't have to do with a specific instantiation of a class, like a User::Load() function or something, or when using the Factory pattern, etc.

Link to comment
Share on other sites

Thanks DarkWater, that makes sense.

 

So if you had a function which would just return the time or date for use in other functions you would make it static because it does not deal with any specific instance of the class?

 

 

<?php

static function getdate() {

    return date('r', time());
}

?>

Link to comment
Share on other sites

  • 3 weeks later...

sample 1 :

<?php

class age{

var $aTrue="true";

var $aFalse="false";

 

function printtrue(){

print "true"; // content manual

}

 

function printfalse(){

$this->aFalse; // content variable

}

}

 

$ageB=new age;

 

class dispAge extends age{

function dispTrue(){

print $age::printtrue(); // disp "true";

}

function dispFalse(){

global $ageB;

print $ageB->printfalse(); // disp "false";

}

}

?>

Link to comment
Share on other sites

When would you use a function from a class without instantiate the class?

 

A class cannot be instantiated. That's like saying "I'm building this blueprint". Instead, you say "I'm building a house, using this blueprint". In OOP, you instantiate an object, using a class.

 

Try to avoid static methods. In some cases they're warranted. In most cases, they're not.

Link to comment
Share on other sites

Explicitly scoping a method call to a class can be used in inheritance.

Say you're overloading a parent's method in a child class, but you want to still use the parent method in your child's method.

If you try calling that same method as you normally would, you would just get a infinite recursion (probably resulting in stack overflow).

But if you explicitly scope the method call (inside the class), the interpreter knows to call the parent's function.

Example:

<?php
class Dad
{
function format($str)
{
	return "[ $str ]";
}
}
class Child extends Dad
{
function format($str)
{
	//Calling $this->format($str) would call this same function, but scoping it to the parent will not
	return "{{ " . parent::format($str) . " }}";
}
}
$timmy = new Child();
echo $timmy->format("something");
?>

It's like explicit scoping in C++ in that you don't put the $this-> reference (since you can't, i.e. $timmy->Dad::format("something") is apparently illegal). You call it as you would a static, but it still retains the $this reference - which can create problems with other functions if you don't declare them as 'static' functions... but that goes along with coding practices as 448191 pointed out.

 

Probably more than you wanted know. Meh.

Link to comment
Share on other sites

This has been a good thread. Thanks to everyone for posting about static methods and when/how to use them. I think it is a very overlooked feature in PHP and I'm glad to see it getting some publicity here. Great stuff!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.