parent
f40d45d0d3
commit
535c14668d
@ -0,0 +1,2 @@ |
||||
composer.lock |
||||
vendor/ |
||||
@ -1,26 +0,0 @@ |
||||
<?php |
||||
interface HasAuthors { |
||||
public string $credits { get; } |
||||
public Author $mainAuthor { get; set; } |
||||
} |
||||
|
||||
class Author { |
||||
public function __construct(public private(set) string $name) {} |
||||
} |
||||
|
||||
class Book implements HasAuthors { |
||||
public string $credits { |
||||
get => "Authors: David Smith\n"; |
||||
} |
||||
|
||||
public Author $mainAuthor { |
||||
get => new Author("Smith"); |
||||
set => throw new Exception("Main author cannot be changed."); |
||||
} |
||||
} |
||||
|
||||
$book = new Book(); |
||||
echo $book->credits; |
||||
|
||||
$author = $book->mainAuthor; |
||||
echo $author->name; |
||||
@ -1,12 +0,0 @@ |
||||
<?php |
||||
|
||||
require "libs/autoload.php"; |
||||
|
||||
$lazyA = new libs\LazyObject(ex\MyClass::class); |
||||
$objectA = $lazyA->proxy(301); |
||||
// The object is initialized only when accessed |
||||
echo $objectA->someMethod() . "\n"; |
||||
|
||||
$lazyB = new libs\LazyObject(ex\Example2::class); |
||||
$objectB = $lazyB->proxy(); |
||||
$objectB->hi(); |
||||
@ -1,3 +0,0 @@ |
||||
<?php |
||||
$name ??= "Bobby"; |
||||
echo $name; |
||||
@ -0,0 +1,5 @@ |
||||
{ |
||||
"require": { |
||||
"phpstan/phpstan": "^2.1" |
||||
} |
||||
} |
||||
@ -1,6 +0,0 @@ |
||||
<?php |
||||
namespace ex; |
||||
|
||||
class Example2 { |
||||
public function hi() { echo "Hi!"; } |
||||
} |
||||
@ -1,9 +0,0 @@ |
||||
<?php |
||||
namespace ex; |
||||
|
||||
class MyClass { |
||||
public function __construct(public int $num) {} |
||||
public function someMethod() { |
||||
return $this->num; |
||||
} |
||||
} |
||||
@ -0,0 +1,5 @@ |
||||
parameters: |
||||
level: max |
||||
|
||||
paths: |
||||
- src |
||||
@ -0,0 +1,32 @@ |
||||
<?php |
||||
interface HasAuthors { |
||||
public string $credits { get; } |
||||
public Author $mainAuthor { get; set; } |
||||
} |
||||
|
||||
class Author { |
||||
public function __construct(public readonly string $name) {} // Using readonly instead of private(set) |
||||
} |
||||
|
||||
class Book implements HasAuthors { |
||||
private Author $_mainAuthor; // Explicit backing field |
||||
|
||||
public function __construct() { |
||||
$this->_mainAuthor = new Author("Smith"); // Initialize |
||||
} |
||||
|
||||
public string $credits { |
||||
get => "Authors: David Smith\n"; |
||||
} |
||||
|
||||
public Author $mainAuthor { |
||||
get => $this->_mainAuthor; // Now reads the stored value |
||||
set => throw new \Exception("Main author cannot be changed."); |
||||
} |
||||
} |
||||
|
||||
$book = new Book(); |
||||
echo $book->credits; // "Authors: David Smith\n" |
||||
|
||||
$author = $book->mainAuthor; |
||||
echo $author->name; // "Smith" |
||||
@ -0,0 +1,19 @@ |
||||
<?php |
||||
|
||||
require "libs/autoload.php"; |
||||
|
||||
$lazyA = new libs\LazyObject(ex\MyClass::class); |
||||
|
||||
$runA = (function(int $i) use ($lazyA) { |
||||
$objectA = $lazyA->proxy($i); |
||||
// The object is initialized only when accessed |
||||
$data = $objectA->someMethod(); |
||||
$data = new libs\Scalar()->intoString($data); |
||||
echo $data; |
||||
}); |
||||
|
||||
$lazyB = new libs\LazyObject(ex\Example2::class); |
||||
$objectB = $lazyB->proxy(); |
||||
$objectB->hi(); |
||||
|
||||
$runA(800); |
||||
@ -0,0 +1,8 @@ |
||||
<?php |
||||
require "libs/autoload.php"; |
||||
|
||||
$name ??= "Bobby"; |
||||
|
||||
$name = new libs\Scalar()->intoString($name); |
||||
|
||||
echo $name; |
||||
@ -0,0 +1,6 @@ |
||||
<?php |
||||
namespace ex; |
||||
|
||||
class Example2 { |
||||
public function hi(): void { echo "Hi! \n"; } |
||||
} |
||||
@ -0,0 +1,9 @@ |
||||
<?php |
||||
namespace ex; |
||||
|
||||
class MyClass { |
||||
public function __construct(public int $num) { echo "Loaded \n"; } |
||||
public function someMethod(): int { |
||||
return $this->num; |
||||
} |
||||
} |
||||
@ -0,0 +1,11 @@ |
||||
<?php |
||||
require "libs/autoload.php"; |
||||
|
||||
$names = new libs\Collection(['Mike','Lisa','Jenny']); |
||||
|
||||
$names = $names->all(); |
||||
|
||||
foreach($names as $name) { |
||||
$name = new libs\Scalar()->intoString($name); |
||||
echo "Hello, " . $name . PHP_EOL; |
||||
} |
||||
@ -0,0 +1,3 @@ |
||||
Hello, |
||||
you@ |
||||
guys! |
||||
@ -0,0 +1,23 @@ |
||||
<?php |
||||
|
||||
namespace libs; |
||||
|
||||
/** |
||||
* @template TItem |
||||
*/ |
||||
final readonly class Collection { |
||||
/** |
||||
* @param array<int, TItem> $items |
||||
*/ |
||||
public function __construct( |
||||
private array $items |
||||
) { |
||||
} |
||||
|
||||
/** |
||||
* @return array<int, TItem> |
||||
*/ |
||||
public function all(): array { |
||||
return $this->items; |
||||
} |
||||
} |
||||
@ -0,0 +1,13 @@ |
||||
<?php |
||||
|
||||
namespace libs; |
||||
|
||||
class Scalar { |
||||
public function intoString(mixed $name): string { |
||||
if (is_scalar($name) || (is_object($name) && method_exists($name, '__toString'))) { |
||||
return (string)$name; |
||||
} else { |
||||
return ''; // or handle the error case |
||||
} |
||||
} |
||||
} |
||||
Loading…
Reference in new issue