A collection of PHP 7 classes useful for code under construction.
Branch | Status |
---|---|
master | |
dev |
This library is registered on packagist, so you can easily include it in your project using composer.
composer require jpuck/phpdev
Don't forget to require the autoloader in your script.
<?php
require_once 'vendor/autoload.php';
Checks whether a string begins with a string.
$haystack = 'test';
$needle = 'te';
if(Functions::strbegins($haystack, $needle)){
echo "$haystack begins with $needle";
}
Tabulator wrapper for print_r
replaces 8 spaces with a tab.
use jpuck\phpdev\Functions as jp;
$array = [
'first' =>
[
'second' =>
[
'third' =>
[
'forth' =>
[
'fifth' => 5
]
]
]
]
];
print_r($array);
jp::print_rt($array);
Example displayed on console with tabs set to 4 spaces:
Array ( [first] => Array ( [second] => Array ( [third] => Array ( [forth] => Array ( [fifth] => 5 ) ) ) ) ) Array ( [first] => Array ( [second] => Array ( [third] => Array ( [forth] => Array ( [fifth] => 5 ) ) ) ) )
Format arrays using var_export
with tabs, square brackets, and
no trailing spaces after arrows to newlines.
use jpuck\phpdev\Functions as jp;
$array = [
'first' =>
[
'second' =>
[
'third' =>
[
'forth' =>
[
'fifth' => 5
]
]
]
]
];
var_export($array);
jp::arr_export($array);
Example displayed on console with tabs set to 4 spaces:
array ( 'first' => array ( 'second' => array ( 'third' => array ( 'forth' => array ( 'fifth' => 5, ), ), ), ), ) [ 'first' => [ 'second' => [ 'third' => [ 'forth' => [ 'fifth' => 5, ], ], ], ], ]
NOTICE: The documentation on using this function with objects is clear.
Note: To be able to evaluate the PHP generated by
var_export()
, all processed objects must implement the magic__set_state
method. Note that StdClass does not implement__set_state()
.
$function = function(){};
$a = [
'object' => new stdClass,
'function' => $function,
];
$b = var_export($a, true);
eval("print_r($b);");
The above code will result in the following errors:
PHP Fatal error: Uncaught Error: Call to undefined method stdClass::__set_state()
PHP Fatal error: Uncaught Error: Call to undefined method Closure::__set_state()
This is a dangerously powerful script for dropping everything from a Microsoft SQL Server database dbo schema.
- non-system stored procs
- views
- functions
- tables (regardless of Primary/Foreign Key constraints)
use jpuck\phpdev\Exceptions\Unimplemented;
class MyClass {
public function foo() {
// completed code
return true;
}
/**
* @throws Unimplemented
*/
public function bar() {
// work in progress
throw new Unimplemented(__METHOD__);
return true;
}
}