-
-
Save brzuchal/c45010f0dd20642b470eeee8b9c56c5f to your computer and use it in GitHub Desktop.
Simplified PHP Packages
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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 {} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
package MyVendor\MyLibrary; // triggers autoload of "MyVendor\MyLibrary" package and load "__package__.php" file | |
// namespace; not needed in root package namespace | |
class Foo {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
package MyVendor\MyLibrary; | |
namespace FooBar; | |
class Baz {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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