yoursurrogategod Posted October 24, 2012 Share Posted October 24, 2012 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? Quote Link to comment https://forums.phpfreaks.com/topic/269869-calling-static-methods-how/ Share on other sites More sharing options...
ManiacDan Posted October 24, 2012 Share Posted October 24, 2012 self::functionName() Quote Link to comment https://forums.phpfreaks.com/topic/269869-calling-static-methods-how/#findComment-1387521 Share on other sites More sharing options...
yoursurrogategod Posted October 24, 2012 Author Share Posted October 24, 2012 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? Quote Link to comment https://forums.phpfreaks.com/topic/269869-calling-static-methods-how/#findComment-1387527 Share on other sites More sharing options...
akphidelt2007 Posted October 24, 2012 Share Posted October 24, 2012 You can change your error reporting to not show STRICT errors or change private function samplePrint to private static function samplePrint Quote Link to comment https://forums.phpfreaks.com/topic/269869-calling-static-methods-how/#findComment-1387536 Share on other sites More sharing options...
MDCode Posted October 24, 2012 Share Posted October 24, 2012 You never want to cover up errors, simply fix them Quote Link to comment https://forums.phpfreaks.com/topic/269869-calling-static-methods-how/#findComment-1387537 Share on other sites More sharing options...
akphidelt2007 Posted October 24, 2012 Share Posted October 24, 2012 (edited) You never want to cover up errors, simply fix them True, but you can live without E_STRICT errors Edited October 24, 2012 by akphidelt2007 Quote Link to comment https://forums.phpfreaks.com/topic/269869-calling-static-methods-how/#findComment-1387538 Share on other sites More sharing options...
yoursurrogategod Posted October 24, 2012 Author Share Posted October 24, 2012 You never want to cover up errors, simply fix them How would I do that in this case? I _need_ to call samplePrint, any way to get around this issue? Quote Link to comment https://forums.phpfreaks.com/topic/269869-calling-static-methods-how/#findComment-1387547 Share on other sites More sharing options...
ManiacDan Posted October 24, 2012 Share Posted October 24, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/269869-calling-static-methods-how/#findComment-1387548 Share on other sites More sharing options...
Barand Posted October 24, 2012 Share Posted October 24, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/269869-calling-static-methods-how/#findComment-1387554 Share on other sites More sharing options...
akphidelt2007 Posted October 24, 2012 Share Posted October 24, 2012 When I ran this code it printed This is a simple sample print! as expected Probably because you have E_STRICT errors turned off. If you put in error_reporting(E_ALL | E_STRICT) it will trigger the error. Quote Link to comment https://forums.phpfreaks.com/topic/269869-calling-static-methods-how/#findComment-1387557 Share on other sites More sharing options...
Barand Posted October 24, 2012 Share Posted October 24, 2012 I had error_reporting(E_ALL); I thought I'd read somewhere that E_ALL now included E_STRICT. Quote Link to comment https://forums.phpfreaks.com/topic/269869-calling-static-methods-how/#findComment-1387560 Share on other sites More sharing options...
Christian F. Posted October 24, 2012 Share Posted October 24, 2012 Only from PHP 5.4 and onwards, if you're using 5.3.x it's not. So I'd venture a guess that you're on 5.3. Quote Link to comment https://forums.phpfreaks.com/topic/269869-calling-static-methods-how/#findComment-1387563 Share on other sites More sharing options...
Barand Posted October 24, 2012 Share Posted October 24, 2012 You're right - 5.3.10 Quote Link to comment https://forums.phpfreaks.com/topic/269869-calling-static-methods-how/#findComment-1387573 Share on other sites More sharing options...
yoursurrogategod Posted October 25, 2012 Author Share Posted October 25, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/269869-calling-static-methods-how/#findComment-1387660 Share on other sites More sharing options...
yoursurrogategod Posted October 25, 2012 Author Share Posted October 25, 2012 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(); ?> Quote Link to comment https://forums.phpfreaks.com/topic/269869-calling-static-methods-how/#findComment-1387665 Share on other sites More sharing options...
ManiacDan Posted October 25, 2012 Share Posted October 25, 2012 Why would you do it that way? Static methods exist so you don't have to instantiate the class. Why would you then instantiate the class inside of a static method? What's the purpose of that? Quote Link to comment https://forums.phpfreaks.com/topic/269869-calling-static-methods-how/#findComment-1387689 Share on other sites More sharing options...
yoursurrogategod Posted October 25, 2012 Author Share Posted October 25, 2012 Why would you do it that way? Static methods exist so you don't have to instantiate the class. Why would you then instantiate the class inside of a static method? What's the purpose of that? If you really need to be able to use a dynamic function from inside a static method, then that's one way. Quote Link to comment https://forums.phpfreaks.com/topic/269869-calling-static-methods-how/#findComment-1387720 Share on other sites More sharing options...
akphidelt2007 Posted October 25, 2012 Share Posted October 25, 2012 If you really need to be able to use a dynamic function from inside a static method, then that's one way. 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. Quote Link to comment https://forums.phpfreaks.com/topic/269869-calling-static-methods-how/#findComment-1387735 Share on other sites More sharing options...
yoursurrogategod Posted October 25, 2012 Author Share Posted October 25, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/269869-calling-static-methods-how/#findComment-1387737 Share on other sites More sharing options...
ManiacDan Posted October 25, 2012 Share Posted October 25, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/269869-calling-static-methods-how/#findComment-1387738 Share on other sites More sharing options...
akphidelt2007 Posted October 25, 2012 Share Posted October 25, 2012 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(); Quote Link to comment https://forums.phpfreaks.com/topic/269869-calling-static-methods-how/#findComment-1387740 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.