PHP code example of typesetsh / dynamic-property-access-assume-type

1. Go to this page and download the library: Download typesetsh/dynamic-property-access-assume-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/ */

    

typesetsh / dynamic-property-access-assume-type example snippets



/**
 * Some file doc comment.
 *
 * @see LICENSE.md
 */
declare(strict_types=1);

namespace Typesetsh\Psalm\DynamicPropertyAccessAssumeType\Example;

/**
 * @universal-object-crate int|null
 */
class M extends \stdClass
{
}

/**
 * @property int $age
 */
class N extends M
{
    public string $name = '';
}

$m = new M();
$m->age = 34;

$n = new N();
$n->name = 'John';
$n->age = 34;

return [$m, $n];





/**
 * Some file doc comment.
 *
 * @see LICENSE.md
 */
declare(strict_types=1);

namespace Typesetsh\DynamicProperties\Example;

/**
 * @allow-array-casting
 */
class Data extends \stdClass
{
    public string $name = '';
    public int $age = 0;
}

$data = new Data();

foreach ((array) $data as $key => $value) {
    echo $key, $value;
}




/**
 * @dynamic-property-access-assume-type
 */
class B
{
    public string $name = '';
    
    public function __get(string $name): int
    {
        throw new \RuntimeException('No such property');
    }
}

/**
 * @dynamic-property-access-assume-type
 */
class A
{
    public function __get(string $name): B
    {
        throw new \RuntimeException('No such property');
    }
}

// dynamic property access
$prop1 = (string) rand(1000, 9999);
$prop2 = (string) rand(1000, 9999);

$a = new A();

return $a->{$prop1}->{$prop2};