1. Go to this page and download the library: Download su-rin/type 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/ */
su-rin / type example snippets
interface C{}
class B{}
class A extends B implements C{}
$arrC = new TypeArray(C::class);
$arrB = new TypeArray(B::class);
$arrA = new TypeArray(A::class);
$arrN = new TypeArray("string"); //string|integer|double|boolean here
//$arrE = new TypeArray("SomeThingNotExists"); //error!!! class not found.
//init array like this
$arrIA = new TypeArray(A::class,[new A(),new A()]; //ok
//$arrIAE = mew TypeArray(A::class,[new B(),123,"abc"]) //error!!! incompatible type.
$arrC[] = new A; //can assign like this case.
$arrB[] = new A; //ok
$arrA[] = new A; //ok
//$arrA[] = 123; //error!!! incompatible type.
//$arrA[] = new B; //error!!! incompatible type.
$arrB[] = new A; //ok
//$arrN[] = .12; //error!!! incompatible type.
$arrA->checkType(B::class); //ok
//$arrB->checkType(A::class); //error!!! incompatible type.
print 'ok';
class A implements JsonSerializable
{
public function get():int
{
return 1;
}
public function jsonSerialize()
{
return 1;
}
}
//if you write comment like this
/** @var A[]|TypeArray $arr */
$arrA = new TypeArray(A::class);
$arrA[] = new A();
$arrA[] = new A();
//method get will hint in IDE. (A)
$arrA[0]->get();
//method checkType too. (TypeArray)
$arr->checkType(A::class);
//json support
json_encode($arrA); // [1, 1]
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.