Legacy PHP4 behavior is still there in current PHP versions. Could lead to debug nightmare.
class Test
{
public function test()
{
echo 'Hello!';
// it could be less innocent i.e. format disk ;)
}
}
$test = new Test();
// runs Test::test()
Very useful in case when foreign constraints aren't working.
SELECT TABLE_NAME
FROM information_schema.TABLES
WHERE TABLE_SCHEMA='my_db_name'
AND TABLE_TYPE='BASE TABLE'
AND ENGINE IN ('MARIA','MyISAM')
Sometimes you don't want to spoil API in order to test private or protected method of a class.
A good example is a public method that calls one of two private methods depending on the presence of PECL extension. Running tests twice with different PHP configuration is a waste of time. Making methods public isn't good for API.
function callPrivateMethod($object, $method, $args)
{
$classReflection = new \ReflectionClass(get_class($object));
$methodReflection = $classReflection->getMethod($method);
$methodReflection->setAccessible(true);
$result = $methodReflection->invokeArgs($object, $args);
$methodReflection->setAccessible(false);
return $result;
}
$myObject = new MyClass();
callPrivateMethod($myObject, 'hello', ['world']);
The code determines number of significant lines in the code. It skips newlines and comments. The plan is to skip doc-comments as well.
Can you improve it? Make it simpler and faster? Send your revisions. I think mine is the simplest one.
public function valid_length($code) {
$count = 0;
foreach (explode("\n", $code) as $line) {
$line = trim($line);
$line !== '' &&
$line !== '{' &&
$line !== '}' &&
preg_match_all('~(?://|[#])~', $line) !== 1 ? $count ++ : false;
}
if ($count > 18) {
$this->form_validation->set_message('valid_length', 'Your stash has more than 18 lines');
return false;
} else {
return true;
}
}
Most probably PHP 7 is going to have anonymous classes. Above is my use case for these. What are yours?
Would be glad to receive some ideas as revisions.
$image = new class {
public width = null;
public height = null;
public function __construct($filename) {
list($width, $height) = getimagesize($filename);
$this->width = $width;
$this->height = $height;
}
public function resize($width, $height) {
// some code
}
};
$image->resize(500, 500);
In C++, C# etc. you know what will be returned from a method. No surprises such as getting a string instead of expected int.
Using PHP 7 you can finally specify return type!
class Post extends CI_Model {
public function __construct() {
$this->load->database();
$this->load->helper(['url', 'date']);
}
/**
* Getting post by ID
* @param $id
* @return array
*/
public function get_post_by_id($id): array {
$query = $this->db->where('id', $id)->get('post');
return ! is_null($query->row()) ? $query->row() : [];
}
}
I've been searching for a good way to form a tree. I think this one is ideal. Can you make it better?
private function buildTree($data)
{
$tree = [];
$references = [];
foreach ($data as $id => &$node) {
$references[$node['id']] = &$node;
$node['children'] = [];
if (is_null($node['parent_id'])) {
$tree[] = &$node;
} else {
$references[$node['parent_id']]['children'][] = &$node;
}
}
return $tree;
}