Jump to content

PHP's magic methods?

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
3 replies to this topic

#1
FireGator

FireGator

    Learning Programmer

  • Members
  • PipPipPip
  • 37 posts
On my studying of the PHP official manual I notice the OO manual section does not mention use of many of the magic functions PHP OO povides (which I know they have elsewhere). In C++ class constructors and ~deconstructors are used in object classes, or basically in C, (with a class _factory and cleanup method). What methods should I gain knowledge to use in object oriented programming with PHP?

My own example of all I know:
class Foo {

    public $foo;

    private $debug;

    private function doSomething($args) {

          return $args;

    }

    public function __construct($args) {

         $this->debug[] = "Constructor called";

         $this->foo = $this->doSomething($args);

    }

    public function __destruct($args) {

        $this->debug[] = "Deconstructor called";

        unset($this->foo);

    }

}


#2
Orjan

Orjan

    Writes binary right handed and hex left handed

  • Moderators
  • 3,299 posts
there are a few more:
PHP: Magic Methods - Manual

__get & __set : automatic getters and setters, they can even be virtual! you can use this methods and tell your class how to read and set the variables, maybe towards a database directly if you wish
__sleep & __wakeup : what to do on serialize & unserializing the object
__toString : just what to do when trying to output the object as a string
__invoke : cool feature; what to do if someone tries to call the object as it was an function!

all others are in the php documentation page above.

there are also some other magic functions that isn't directly associated with object oriented...

__autoload : make your own routine to include files automatically instead of have that long list of includes. This way, a class file isn't included until it's needed...
PHP: Autoloading Classes - Manual
__________________________________________
I study Information Systems at Karlstad University when I'm not on CodeCall

#3
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,124 posts
Orjan put it very well, there are a few others you may be interested in for the sake of experience, I will list an interface of them
interface AllMagicMethods {
    // accessing private or undefined properties:
    public function __get($argField);
    public function __set($argField, $value);
    public function __isset($argField);
    public function __unset($argField);

    // calling undefined or private methods: 
    public function __call($funcName, $args);
    // static call currently as of PHP 5.3:
    public static function __callStatic($funcName, $args); 

    // Returns the string representation of the object (i.e. echo $obj, (string) $obj)
    public function __toString();

    // on serialize() to save the object and unserialize()
    public function __sleep();
    public function __wakeup();

    // on var_export()
    public static function __set_state($array);

    // invoke the object as if it were a function (i.e. $obj($arg, $args))
    public function __invoke($arguments, $...);

}
I would highly recommend reading fully the manual linked for example uses, bugs and workarounds.
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#4
FireGator

FireGator

    Learning Programmer

  • Members
  • PipPipPip
  • 37 posts
Thank you both, alot. This means a great deal for my studies, if I got any more questions I'll ask.