jpuck/phpdev

A collection of classes useful for code under construction.

1.9.1 2017-12-14 03:41 UTC

This package is not auto-updated.

Last update: 2024-04-13 16:57:02 UTC


README

A collection of PHP 7 classes useful for code under construction.

Branch Status
master Build Status
dev Build Status

Getting Started

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';

Functions

bool strbegins(String $haystack, String $needle)

Checks whether a string begins with a string.

$haystack = 'test';
$needle   = 'te';
if(Functions::strbegins($haystack, $needle)){
	echo "$haystack begins with $needle";
}

mixed print_rt ( mixed $expression [, bool $return = false ] )

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
                )

            )

        )

    )

)

mixed arr_export ( array $array [, bool $return = false ] )

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()

null CleanMsSQLdb (PDO $pdo)

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)

Exceptions

Unimplemented Method

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;
	}
}