PHP Classes

Stern PHP Type Safety: Make class function calls invoke strict versions

Recommend this page to a friend!
  Info   Documentation   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not yet rated by the usersTotal: 37 All time: 10,956 This week: 660Up
Version License PHP version Categories
stern 1.0MIT/X Consortium ...5Language, PHP 7
Description 

Author

This package can make class function calls invoke strict versions.

It provides a trait that can be used by any class, so when missing functions of the class are called, it can forward the calls to functions with the prefix strict, so PHP 7 based strict type checking of the parameters values passed to the class is performed.

Innovation Award
PHP Programming Innovation award nominee
June 2020
Number 9
PHP 7 introduced the possibility to impose stricter restrictions to the types of the values passed or returned by class functions.

This package provides a simple trait that forces calling stricter versions of PHP class functions, even if applications call past versions of class versions that did not used to be stricter.

Manuel Lemos
Picture of Scott Arciszewski
  Performance   Level  
Name: Scott Arciszewski <contact>
Classes: 37 packages by
Country: United States United States
Age: ???
All time rank: 1173170 in United States United States
Week rank: 22 Up4 in United States United States Up
Innovation award
Innovation award
Nominee: 28x

Winner: 1x

Documentation

Stern

Build Status Latest Stable Version Latest Unstable Version License

Stern lets you built type-safe PHP projects, even if your project's users aren't writing type-safe code.

Requires PHP 7+

Usage

Using Stern is simply:

  1. Make your class use the `SternTrait`.
  2. Rename your methods from `whateverName` to `strictWhateverName`.
  3. Enjoy strict-typing whether your users like it or not.

Example

  <?php
  declare(strict_types=1);
  namespace YourVendor\YourNamespace;

  class YourClassThatUsesStrictTypes
  {
+      use \ParagonIE\Stern\SternTrait;
  
      /.../
  
-     public function foo(string $param = ''): bool
+     public function strictFoo(string $param = ''): bool
      {
      }
  }

Docblock Usability

For better usability (especially with type-aware IDEs like PHPStorm), make sure you use @method docblocks.

  <?php
  declare(strict_types=1);
  namespace YourVendor\YourNamespace;

+   /
+    * @method bool foo(string $param = '')
+    */
  class YourClassThatUsesStrictTypes
  {
+      use \ParagonIE\Stern\SternTrait;
  
      /.../
  
-     public function foo(string $param = ''): bool
+     public function strictFoo(string $param = ''): bool
      {
      }
  }

  Files folder image Files (10)  
File Role Description
Files folder imagesrc (1 file)
Files folder imagetests (1 file, 2 directories)
Accessible without login Plain text file .travis.yml Data Auxiliary data
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file phpunit.xml.dist Data Auxiliary data
Accessible without login Plain text file psalm.xml Data Auxiliary data
Accessible without login Plain text file README.md Doc. Read me

  Files folder image Files (10)  /  src  
File Role Description
  Accessible without login Plain text file SternTrait.php Class Class source

  Files folder image Files (10)  /  tests  
File Role Description
Files folder imagephpunit (1 file)
Files folder imagetest-classes (1 file)
  Accessible without login Plain text file autoload.php Aux. Auxiliary script

  Files folder image Files (10)  /  tests  /  phpunit  
File Role Description
  Accessible without login Plain text file WeakSternTest.php Class Class source

  Files folder image Files (10)  /  tests  /  test-classes  
File Role Description
  Accessible without login Plain text file Weak.php Class Class source

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads Download Rankings  
 100%
Total:37
This week:0
All time:10,956
This week:660Up