Added Magic Methods (#31344)

* Added Magic Methods

* Update index.md
pull/32078/head^2
Daniele Pedone 2019-03-28 23:33:08 +01:00 committed by Randell Dawson
parent d123d4a0eb
commit d7736fb8be
1 changed files with 22 additions and 0 deletions

View File

@ -371,3 +371,25 @@ echo json_encode($jack->getFavDrinks());
```
This way of implementing and using class methods to retrieve and update class properties is called encapsulation in Object Oriented Programming. We can also set visibility for class methods just like how we did it for class properties.
### MAGIC METHODS
There are some special methods that Php uses for its own purpose.
Suppose you want to threat an object as a string. You can do that by overriding `__toString()` method into related class:
```
<?php
class Foo {
public function __toString() {
return 'foo';
}
}
$foo = new Foo();
echo $foo;
// "foo"
```
There are many more other [magic methods](http://php.net/manual/en/language.oop5.magic.php).