Skip to content

Instantly share code, notes, and snippets.

@brzuchal
Last active July 30, 2019 18:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brzuchal/c45010f0dd20642b470eeee8b9c56c5f to your computer and use it in GitHub Desktop.
Save brzuchal/c45010f0dd20642b470eeee8b9c56c5f to your computer and use it in GitHub Desktop.
Simplified PHP Packages
<?php // file in root library/package directory
package MyVendor\MyLibrary {
// declare() not need to look like a function it's always assign operation: key => value
declare strict_types = 1, strict_operators = 1;
use SomeVendor\AutoloaderLibrary:Psr4Autoloader; // class loaded from other package
declare class_autoload = new Psr4Autoloader(__PACKAGE__, __DIR__ . '/src');
declare function_autoload = static function (string $functionName): void {
@require_once __DIR__ . '/src/functions.php';
};
// dependency expectations
expect SomeVendor\AutoloaderLibrary;
// export only public symbols
export class Foo; // class loaded through autoload
export class FooBar\Baz; // class loaded through autoload
export const FOO_CONST; // const declared in package root namespace inlined above
export function my_library_function; // function loaded through autoload and declared in "functions.php"
// group export
export {
class Foo,
class FooBar\{Baz, BazBaz},
const FOO_CONST,
function my_library_function,
};
namespace {
const FOO_CONST = 1;
function my_another_library_function(): void {}
}
}
version: "8.0"
name: MyVendor\MyLibrary
autoload:
classes: !psr4 "src/"
functions: &file !require "src/functions.php"
constants: *file
declare:
strict_types: 1
strict_operators: 1
<?php
package MyVendor\MyLibrary; // triggers autoload of "MyVendor\MyLibrary" package and load "__package__.php" file
// namespace; not needed in root package namespace
class Foo {}
<?php
package MyVendor\MyLibrary;
namespace FooBar;
class Baz {}
<?php
package MyVendor\MyLibrary;
namespace FooBar;
class BazBaz {}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment