__get() example via Zandstra

Asked
Active3 hr before
Viewed126 times

3 Answers

example
90%

Meta Stack Overflow , Stack Overflow Public questions & answers , Stack Overflow help chat ,Thanks for contributing an answer to Stack Overflow!

The actual functionality of magical methods is totally up to the developer to handle in any way they see fit. The difference is in how they are invoked.

$obj - > property; // invokes __get(), if defined
$obj - > method(); // invokes __call(), if defined
$obj(); // invokes __invoke() if defined (php 5.3+)

With the php magic functions, you can do some very cool things and you can write code that is more extensible by using these special invocations. Something I like to do:

class application {
   public
   function __call($_, $_) {
      throw new Exception("PUT and DELETE not supported");
   }
   public
   function POST() {
      //update data
      header("Location: $_SERVER[PHP_SELF]", true, 303);
   }
   public
   function GET() {
      //display data
   }
   public static
   function inst() {
      return new self;
   }
}

application::inst() - > $_SERVER['REQUEST_METHOD']();
load more v
88%

Example #6 Using __debugInfo(), This method is called by var_dump() when dumping an object to get the properties that should be shown. If the method isn't defined on an object, then all public, protected and private properties will be shown. ,Example #4 Using __invoke(),Example #5 Using __set_state()

Hello
load more v
72%

__destruct() Method: In the below example, the MagicMethod class has a magic method __destruct() that gets called automatically when the object of MagicMethod destroys. ,__construct() Method: In the below example, the MagicMethod class has a magic method __construct() and it is called every time when a new object of MagicMethod class is created., PHP magic methods are special methods that are called automatically when certain conditions are met. There are several magic methods in  PHP. Every magic method follows certain rules –,__debugInfo() Method: This method is used when the var_dump() function is called with object as a parameter. This method should return an array containing all the variables which may be useful in debugging.

load more v

Other "example-undefined" queries related to "__get() example via Zandstra"