1. Go to this page and download the library: Download headzoo/core library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
headzoo / core example snippets
echo Strings::camelCaseToUnderscore("CamelCaseString");
// Outputs: "camel_case_string"
echo Strings::camelCaseToUnderscore("MaryHadALittleLamb");
// Outputs: "mary_had_a_little_lamb"
$is = Strings::startsUpper("Welcome my son, welcome to the machine.");
var_dump($is);
// Output: bool(true);
$is = Strings::startsUpper("you've been in the pipeline, filling in time");
var_dump($is);
// Output: bool(false)
// Truncating a string at the end.
echo Strings::truncate("Mary had a little lamb, whose fleece was white as snow.", 20, Strings::TRUNC_END);
// Outputs: "Mary had a little..."
// Truncating a string at the start.
echo Strings::truncate("Mary had a little lamb, whose fleece was white as snow.", 20, Strings::TRUNC_START);
// Outputs: "...as white as snow."
// Truncating a string in the middle.
echo Strings::truncate("Mary had a little lamb, whose fleece was white as snow.", 20, Strings::TRUNC_MIDDLE);
// Outputs: "Mary ha...e as snow."
$array = [
"headzoo",
"joe",
"sam"
];
echo Arrays::conjunct($array);
// Outputs: headzoo, joe, and sam
// Using a callback to quote the array values.
echo Arrays::conjunct($array, "and", 'Headzoo\Core\Strings::quote');
// Outputs: 'headzoo', 'joe', and 'sam'
$arr = [
0 => [
"username" => "headzoo",
"email" => "[email protected]"
],
1 => [
"username" => "joe",
"email" => "[email protected]"
]
]
$ret = Arrays::column($arr, "username");
// Outputs: ["headzoo", "joe"]
// Testing whether an object is an instance of another.
$is = Objects::isInstance(new stdClass(), stdClass);
var_dump($is);
// Outputs: bool(true)
// Unlike the instanceof operator, the second argument can be a string.
$is = Objects::isInstance(new stdClass(), 'stdClass');
var_dump($is);
// Outputs: bool(true);
// You can even test an array of objects.
$objects = [
new stdClass(),
new stdClass()
];
$is = Objects::isInstance($objects, stdClass);
var_dump($is);
// Outputs: bool(true)
// Capture all errors, and display an error page instead of the usual php
// error message.
$handler = new ErrorHandler();
$handler->handle();
// Setup your own way of handing errors.
$handler = new ErrorHandler();
$handler->setCallback(function($handler) {
lude("template/error_live.php");
});
$handler->handle("live");
// The most basic profiling.
$profiler = new Profiler();
$profiler->start();
... do something here ...
$micro = $profiler->stop();
var_dump($micro);
// Outputs:
// "Profile time for 'default': 0.00030207633972168"
// double(0.00030207633972168)
// This example runs the closure 100 times, and displays the profile results.
Profiler::run(100, true, function() {
... do something here ...
});
// Output:
//
// Total Runs: 100
// Total Time: 0.099596977234
// Average Time: 0.000981624126
// -------------------------------
// Run #1 0.000479936599
// Run #2 0.000968933105
// Run #3 0.000982999801
// Run #4 0.000988006591
// ......
// Run #97 0.000985145568
// Run #98 0.000983953476
// Run #99 0.000997066497
// Run #100 0.000993013382
class DaysEnum
extends AbstractEnum
{
const SUNDAY = "SUNDAY";
const MONDAY = "MONDAY";
const TUESDAY = "TUESDAY";
const WEDNESDAY = "WEDNESDAY";
const THURSDAY = "THURSDAY";
const FRIDAY = "FRIDAY";
const SATURDAY = "SATURDAY";
const __DEFAULT = self::SUNDAY;
}
$day = new DaysEnum("SUNDAY");
echo $day;
echo $day->value();
// Outputs:
// "SUNDAY"
// "SUNDAY"
// The default value is used when not specified.
$day = new DaysEnum();
echo $day;
// Outputs: "SUNDAY"
// The constructor value is not case-sensitive.
$day = new DaysEnum("sUndAy");
echo $day;
// Outputs: "SUNDAY"
// Enum values are easy to compare.
$day_tue1 = new DaysEnum(DaysEnum::TUESDAY);
$day_fri1 = new DaysEnum(DaysEnum::FRIDAY);
$day_tue2 = new DaysEnum(DaysEnum::TUESDAY);
$day_fri2 = new DaysEnum($day_fri1);
var_dump($day_tue1 == DaysEnum::TUESDAY);
var_dump($day_tue1 == $day_tue2);
var_dump($day_fri1 == $day_fri2);
var_dump($day_tue1 == DaysEnum::FRIDAY);
var_dump($day_tue1 == $day_fri1);
// Outputs:
// bool(true)
// bool(true)
// bool(true)
// bool(false)
// bool(false)
// In this example we create a method which requests a web resource using curl.
// We use a SmartCallable instance to ensure the curl resource is closed when
// the method returns, or an exception is thrown.
public function fetch()
{
$curl = curl_init("http://some-site.com");
$sc = SmartCallable::factory(function() use($curl) {
curl_close($curl);
});
$response = curl_exec($curl);
if ($e = curl_error()) {
throw new Exception($e);
}
return $response;
}
// The method could also be written this way.
public function fetch()
{
$curl = curl_init("http://some-site.com");
$sc = SmartCallable::factory("curl_close", $curl);
$response = curl_exec($curl);
if ($e = curl_error()) {
throw new Exception($e);
}
return $response;
}
// In this example you want to insert a row into the database, which may lead to
// a DeadLockException being thrown. The recommended action for dead locks is retrying
// the query. We use a ConfigurableCallable instance to keep trying the query until
// it succeeds.
$link = mysqli_connect("localhost", "my_user", "my_password", "my_db");
$query = new ConfigurableCallable("mysqli_query");
$query->retryOnException(DeadLockException::class);
$result = $query($link, "INSERT INTO `members` ('headzoo')");
// In this example we will call a remote web API, which sometimes takes a few tries
// depending on how busy the remote server is at the any given moment. The remote
// server may return an empty value (null), the API library may thrown an exception,
// or PHP may trigger an error.
$api = new RemoteApi();
$members = new ConfigurableCallable([$api, "getMembers"]);
$members->retryOnException()
->retryOnError()
->retryOnNull();
$rows = $members(0, 10);