Jump to content

Calling Static Methods, How?


yoursurrogategod

Recommended Posts

Hi all, this is what I have at the moment.

<?php
//phpinfo();

class StaticMethodClass {
 private function samplePrint() {
   echo "This is a simple sample print!<br><br>";
 }

 public static function callerFunction() {
   $this::samplePrint();
 }
}

StaticMethodClass::callerFunction();
?>

 

Basically, I'd like to call samplePrint from a public static method, is there any way to go about this?

Link to comment
Share on other sites

self::functionName()

Well, that almost did the trick. This is what I get with the following code:

Code:

<?php
//phpinfo();

class StaticMethodClass {
private function samplePrint() {
echo "This is a simple sample print!<br><br>";
}

public static function callerFunction() {
self::samplePrint();
}
}

StaticMethodClass::callerFunction();
?>

Output:

Strict Standards: Non-static method StaticMethodClass::samplePrint() should not be called statically in /home/ats/Documents/Development/PHP/test.php on line 10

This is a simple sample print!

 

How would I get out of this issue?

Link to comment
Share on other sites

Only static functions can be called statically.  To fix that error, add "static" to your method declaration for the other method.

 

And before you ask: you no cannot call a dynamic method from a static one.

 

Bravo for having error-reporting turned on, well done.

Link to comment
Share on other sites

When I ran this code

 

<?php

class StaticMethodClass {
 private function samplePrint() {
   echo "This is a simple sample print!<br><br>";
 }

 public static function callerFunction() {
   self::samplePrint();
 }
}

StaticMethodClass::callerFunction();
?>

 

it printed This is a simple sample print! as expected

Link to comment
Share on other sites

When I ran this code

 

<?php

class StaticMethodClass {
private function samplePrint() {
 echo "This is a simple sample print!<br><br>";
}

public static function callerFunction() {
 self::samplePrint();
}
}

StaticMethodClass::callerFunction();
?>

 

it printed This is a simple sample print! as expected

It's possible that you did not have all error reporting turned on.

Link to comment
Share on other sites

Only static functions can be called statically. To fix that error, add "static" to your method declaration for the other method.

 

And before you ask: you no cannot call a dynamic method from a static one.

 

Bravo for having error-reporting turned on, well done.

And calling dynamic inside of static methods is possible.

 

ManiacDan, I did find this "work-around" to the problem that I'm having. Basically, you instantiate the class inside of a static method and you can gain functionality to the class. I realize that the static method would need a setter method to pass in variables that the other class might need, but hey, not too shabby.

<?php
//phpinfo();

class StaticMethodClass {
public function simpleMethod() {
self::samplePrintStuff();
echo "And yet more printing!<br><br>";
}

public function weWillPrintStuff() {
echo "Still not enough outputs and other goodies!<br><br>";
}

private static function samplePrintStuff() {
self::samplePrint();
}

private static function samplePrint() {
echo "This is a simple sample print!<br><br>";
}

public static function callerFunction() {
self::samplePrintStuff();

$smClass = new StaticMethodClass();
$smClass->weWillPrintStuff();
}
}

StaticMethodClass::callerFunction();

$smc = new StaticMethodClass();
$smc->simpleMethod();
?>

Link to comment
Share on other sites

There's no purpose to that code though. All you are doing is wrapping the instantiation in a static method which defeats the purpose of using static methods. I guess maybe you save 1 line of code.

Not following you. You have a method that you'd like to use. You don't want to create 2 methods (one static, one dynamic) that are exactly the same. So you make a small instance of the class where the static method because you need access to that one method.

 

Can I create an instance of my class elsewhere as opposed to a static method? Sure. But in a case that having an instance is undesirable/impossible, I don't see an alternative.

Link to comment
Share on other sites

 But in a case that having an instance is undesirable/impossible, I don't see an alternative.
So you're saying that this is impossible/undesireable:

$a = new a();
$a->foo();
unset($a);

So instead you make a function bar() on A:

public static function bar() {
  $a = new a();
  $a->foo();
}

Why?  All you're doing is causing a class to nest inside a non-instantiated version of itself.  It's confusing and entirely unnecessary.  All you're doing is adding overhead and seriously confusing code.

Link to comment
Share on other sites

Not following you. You have a method that you'd like to use. You don't want to create 2 methods (one static, one dynamic) that are exactly the same. So you make a small instance of the class where the static method because you need access to that one method.

 

Can I create an instance of my class elsewhere as opposed to a static method? Sure. But in a case that having an instance is undesirable/impossible, I don't see an alternative.

 

You are creating the instance, you are just wrapping that instance in a static method. What you are doing is no different than this... if you removed the static methods.

 


$smClass = new StaticMethodClass();
$smClass->samplePrintStuff();
$smClass->weWillPrintStuff();

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.