Esses são os erros que estou recebendo no terminal:
PHP Fatal error: Uncaught TypeError: Cannot assign Owner to property Account::$owner of type string in C:\xampp\htdocs\Alura PHP\poo\src\Account.php:11 Stack trace: #0 C:\xampp\htdocs\Alura PHP\poo\bank_codes.php(6): Account->__construct(Object(Owner)) #1 {main} thrown in C:\xampp\htdocs\Alura PHP\poo\src\Account.php on line 11
Fatal error: Uncaught TypeError: Cannot assign Owner to property Account::$owner of type string in C:\xampp\htdocs\Alura PHP\poo\src\Account.php:11 Stack trace: #0 C:\xampp\htdocs\Alura PHP\poo\bank_codes.php(6): Account->__construct(Object(Owner)) #1 {main} thrown in C:\xampp\htdocs\Alura PHP\poo\src\Account.php on line 11
Classe Account.php
<?php
class Account
{
private string $owner;
private float $balance = 0;
private static int $getAccountNumbers = 0;
public function __construct(Owner $owner)
{
$this->owner = $owner;
$this->balance = 0;
self::$getAccountNumbers++; //in this line the "self::" command replaces the "Account::" command or the current class name;
}
//DESTRUCT METHOD (This method remove any object that loses the reference within its respective class, in this case, the Account Class);
public function __destruct()
{
self::$getAccountNumbers--;
}
public function withdraw(float $amountToWithdraw) : void
{
if($amountToWithdraw > $this->balance)
{
echo "Amout to withdraw unavaiable.";
return;
}
$this->balance -= $amountToWithdraw;
}
public function deposit(float $amoutToDeposit): void
{
if($amoutToDeposit <= 0 )
{
echo "Enter a valid value.";
return;
}
$this->balance += $amoutToDeposit;
}
public function transfer(float $amountToTransfer, Account $destinyAccount) : void
{
if ($amountToTransfer > $this->balance)
{
echo "Amout to transfer unavaiable.";
return;
}
$this->withdraw($amountToTransfer);
$destinyAccount->deposit($amountToTransfer);
}
public function getBalance(): float
{
return $this->balance;
}
public function getNameOwner(): string
{
return $this->owner->getName();
}
public function getCpfOwner(): string
{
return $this->owner->getCpf();
}
public static function getAccountNumbers(): int
{
return self::$getAccountNumbers;
}
}
Classe Owner.php
<?php
class Owner
{
private string $cpf;
private string $name;
public function __construct( string $cpf, string $name)
{
$this->cpf = $cpf;
$this->validateOwnerName($name);
$this->name = $name;
}
private function validateOwnerName(string $name)
{
if(strlen($name) < 5){
echo "Name must be at least 5 characters long.";
exit();
}
}
// GET METHODS
public function getCpf(): string
{
return $this->cpf;
}
public function getName(): string
{
return $this->name;
}
}
bank_codes.php (executável do banco)
<?php
require_once 'src/Account.php';
require_once 'src/Owner.php';
$accountOne = new Account(new Owner('123.456.789-12', 'John Winchester'));
$accountOne->deposit(500);
$accountOne->withdraw(100);
$accountTwo = new Account(new Owner('123.456.789-13', 'Mariana Antonela'));
new Account(new Owner('123.564.789-12', 'SDasasasas')); // Account created no reference for test the destruct command
echo $accountOne->getCpfOwner() . PHP_EOL;
echo $accountOne->getNameOwner() . PHP_EOL;
echo $accountOne->getBalance() . PHP_EOL;
echo PHP_EOL . Account::getAccountNumbers();
O código está praticamente igual ao do Vinícius, mas não estou conseguindo corrigir esses erros.