Stream Handlers
This feature allows you to extend the “FileSystem” with logic that is quite difficult to perform in other languages. For instance, the MS-Excel Stream handler provides the ability to create a MS Excel file as follows:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
$fp = fopen("xlsfile://tmp/test.xls", "wb");
if (!is_resource($fp)) {
die("Cannot open excel file");
}
$data= array(
array("Name" => "Bob Loblaw", "Age" => 50),
array("Name" => "Popo Jijo", "Age" => 75),
array("Name" => "Tiny Tim", "Age" => 90)
);
fwrite($fp, serialize($data));
fclose($fp);
|
Magic Methods
These are fall-through methods that can be called every time you invoke a method that doesn’t exist as well as assign or read the same property among other things.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
interface AllMagicMethods {
// accessing undefined or invisible (e.g. private) properties
public function __get($fieldName);
public function __set($fieldName, $value);
public function __isset($fieldName);
public function __unset($fieldName);
// calling undefined or invisible (e.g. private) methods
public function __call($funcName, $args);
public static function __callStatic($funcName, $args); // as of PHP 5.3
// on serialize() / unserialize()
public function __sleep();
public function __wakeup();
// conversion to string (e.g. with (string) $obj, echo $obj, strlen($obj), ...)
public function __toString();
// calling the object like a function (e.g. $obj($arg, $arg2))
public function __invoke($arguments, $...);
// called on var_export()
public static function __set_state($array);
}
|
Standard Class as a Neat Container
Now, using an array for holding several attributes can be replace with a standard class, which is particularly helpful in a string while accessing these variables:
1
|
$string = $person['name'] . ' is ' . $person['age'] . ' years old.';
|
vs
1
|
$string = "$person->name is $person->age years old.";
|
Return Value of Include Files
Now, include files have a return value. And it is possible to assign it to a variable.
1
2
3
4
5
6
7
8
9
10
11
12
13
|
// config.php
return array(
'db' => array(
'host' => 'example.org',
'user' => 'usr',
// ...
),
// ...
);
// index.php
$config = include 'config.php';
echo $config['db']['host']; // example.org
|
‘or’ operator has lower precedence than ‘=’
And you can take advantage out of this feature:
1
2
|
$page = (int) @$_GET['page']
or $page = 1;
|
In a situation when the value of the first assignment evaluates to true, the second one will be ignored.
Variable variables and functions
This concept applies to object parameters as well ($some_object->$some_variable);
1
2
3
4
5
6
7
8
9
10
11
12
13
|
$foo = 'bar';
$bar = 'foobar';
echo $$foo; //This outputs foobar
function bar() {
echo 'Hello world!';
}
function foobar() {
echo 'What a wonderful world!';
}
$foo(); //This outputs Hello world!
$$foo(); //This outputs What a wonderful world!
|
Functions with an undefined number of arguments
In PHP 7, func_get_args() provides the ability to use functions with an undefined number of arguments:
1
2
3
4
5
6
7
8
9
10
11
12
|
<?php
function test() {
$args = func_get_args();
echo $args[2]; // will print 'd'
echo $args[1]; // will print 3
}
test(1,3,'d',4);
?>
|
Other important hidden features of PHP 7
- strtr()
- extract()
- range()
- the static keyword
- Then “and print” trick
- minus character in variable names
- HEREDOC syntax
- fast block comments
- array manipulation
보다 사용하기 편해졌고, 여러 규약에서 어느정도 벗어난 것 같으며, 확장성이 더해졌네요.
더 자세히 알아보고 싶으신 분은 이곳을 참고하세요.