Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to declare a method static and nonstatic in PHP?

Tags:

oop

php

Can I declare a method in an object as both a static and non-static method with the same name that calls the static method?

I want to create a class that has a static method "send" and a non-static method that calls the static function. For example:

class test {     private $text;     public static function instance() {         return new test();     }      public function setText($text) {         $this->text = $text;         return $this;     }      public function send() {         self::send($this->text);     }      public static function send($text) {         // send something     } } 

I want to be able to call the function on these two was

test::send("Hello World!"); 

and

test::instance()->setText("Hello World")->send(); 

is it possible?

like image 497
alphanyx Avatar asked Jul 04 '12 15:07

alphanyx


People also ask

What is static and nonstatic in PHP?

Static class contains static variables and static methods whereas instantiated class contains non-static variables and non-static methods. Programs having static classes are hard to test and to extend while programs with non-static classes provide easy testing and extending property.

Can a class contains both static and nonstatic methods?

Static class always contains static members. Non-static class may contain both static and non-static methods. Static class does not contain an instance constructor. Non-static class contains an instance constructor.

Can a method be static and private?

For example, the main method is static because there should only be 1 main method. Static methods can be public or private. The static keyword is placed right after the public/private modifier and right before the type of variables and methods in their declarations.

Can the static method use a nonstatic member?

No. A static method can access only static members and can not access non-static members. A non-static method can access both static as well as non-static members.


2 Answers

You can do this, but it's a bit tricky. You have to do it with overloading: the __call and __callStatic magic methods.

class test {     private $text;     public static function instance() {         return new test();     }      public function setText($text) {         $this->text = $text;         return $this;     }      public function sendObject() {         self::send($this->text);     }      public static function sendText($text) {         // send something     }      public function __call($name, $arguments) {         if ($name === 'send') {             call_user_func(array($this, 'sendObject'));         }     }      public static function __callStatic($name, $arguments) {         if ($name === 'send') {             call_user_func(array('test', 'sendText'), $arguments[0]);         }     } } 

This isn't an ideal solution, as it makes your code harder to follow, but it will work, provided you have PHP >= 5.3.

like image 113
lonesomeday Avatar answered Sep 24 '22 09:09

lonesomeday


I would make a hidden class as the constructor and return that hidden class inside the parent class that has static methods equal to the hidden class methods:

// Parent class  class Hook {      protected static $hooks = [];      public function __construct() {         return new __Hook();     }      public static function on($event, $fn) {         self::$hooks[$event][] = $fn;     }  }   // Hidden class  class __Hook {      protected $hooks = [];      public function on($event, $fn) {         $this->hooks[$event][] = $fn;     }  } 

To call it statically:

Hook::on("click", function() {}); 

To call it dynamically:

$hook = new Hook; $hook->on("click", function() {}); 
like image 27
Taufik Nurrohman Avatar answered Sep 21 '22 09:09

Taufik Nurrohman