メソッドチェーンについて
Q&A
Closed
解決したいこと
https://codelikes.com/php-method-chain/
を参考にメソッドチェーンでPHPのコードを書いていたのですが、
Call to a member function getSpaceString()
が発生しました。解決法をご教授ください
発生している問題・エラー
<?php
// Main.php
// Your code here!
    require_once('Calculate.php');
    class Main{
        
        private static $instance;
        public static function getInstance()
        {
            if (!self::$instance) {
                self::$instance = new self();
            }
            return self::$instance;
        }
        
        public function do()
        {
            try{
                echo Calculate::getInstance()
                    ->setType('+')
                    ->setInt1(10)
                    ->setInt2(20)
                    ->getInt1()
                    ->getSpaceString()
                    ->getType()
                    ->getSpaceString()
                    ->getInt2()
                    ->getSpaceString()
                    ->getEqualString()
                    ->getSpaceString()
                    ->execute()
                    ->getResultValue()
                    .PHP_EOL;
                echo Calculate::getInstance()
                    ->setType('-')
                    ->setInt1(10)
                    ->setInt2(51)
                    ->execute()
                    ->getResultValue()
                    .PHP_EOL;
                echo Calculate::getInstance()
                    ->setType('*')
                    ->setInt1(10)
                    ->setInt2(0)
                    ->execute()
                    ->getResultValue()
                    .PHP_EOL;
                echo Calculate::getInstance()
                    ->setType('/')
                    ->setInt1(10)
                    ->setInt2(2)
                    ->execute()
                    ->getResultValue()
                    .PHP_EOL;
                echo Calculate::getInstance()
                    ->setType('/')
                    ->setInt1(10)
                    ->setInt2(0)
                    ->execute()
                    ->getResultValue()
                    .PHP_EOL;
            } catch(Exception $e){
                echo $e->getMessage();
            }
        }
    }
    
    Main::getInstance()
    ->do();
    
?>
<?php
// Calculate.phpphp
class Calculate
{
    private static $instance;
    protected int    $int1;
    protected int    $int2;
    protected string $type;
    private mixed  $resultValue;
    protected const EQUAL_STRING = '=';
    protected const SPACE_STRING = ' ';
    public static function getInstance()
    {
        if (!self::$instance) {
            self::$instance = new self();
        }
        return self::$instance;
    }
    /**
     * 
     * @param string $type
     * @return self
     */
    public function setType(string $type): self
    {
        $this->resultValue = 0;
        $this->type = $type;
        return $this;
    }
    /**
     * 
     * @param int $int1
     * @return self
     */
    public function setInt1(int $int1): self
    {
        $this->int1 = $int1;
        return $this;
    }
    /**
     * 
     * @param int $int2
     * @return self
     */
    public function setInt2(int $int2): self
    {
        $this->int2 = $int2;
        return $this;
    }
    /**
     * 
     * @return int $int1
     */
    public function getInt1(): int
    {
        return $this->int1;
    }
    /**
     * 
     * @return int $int2
     */
    public function getInt2(): int
    {
        return $this->int2;
    }
    /**
     * 
     * @return string $type
     */
    public function getType(): string
    {
        return $this->type;
    }
    /**
     * 
     * @return mixed $resultValue
     */
    public function getResultValue(): mixed
    {
        return $this->resultValue;
    }
    /**
     * 
     * @return string
     */
    public function getEqualString(): string
    {
        return self::EQUAL_STRING;
    }
    /**
     * 
     * @return string
     */
    public function getSpaceString(): string
    {
        return self::SPACE_STRING;
    }
    /**
     * 
     * @return self
     */
    public function execute(): self
    {
        switch ($this->type) {
            case '+':
                $this->resultValue = $this->add();
                break;
            case '-':
                $this->resultValue = $this->sub();
                break;
            case '*':
                $this->resultValue = $this->cross();
                break;
            case '/':
                $this->resultValue = $this->div();
                break;
            default:
                throw new Exception('+,-,*,/以外の演算子が入力されました。');
                break;
        }
        return $this;
    }
    /**
     * 
     * @return int
     */
    private function add(): int
    {
        return $this->int1 + $this->int2;
    }
    /**
     * 
     * @return int
     */
    private function sub(): int
    {
        return $this->int1 - $this->int2;
    }
    /**
     * 
     * @return int
     */
    private function cross(): int
    {
        return $this->int1 * $this->int2;
    }
    /**
     * 
     * @return float
     */
    private function div(): float
    {
        if ($this->int2 === 0) {
            throw new Exception('割る数は0以外で入力してください。');
        }
        return $this->int1 / $this->int2;
    }
}
上記コードを実行したところ
Fatal error: Uncaught Error: Call to a member function getSpaceString() on int in C:\xampp\htdocs\calc\main.php:24 Stack trace: #0 C:\xampp\htdocs\calc\main.php(74): Main->do() #1 {main} thrown in C:\xampp\htdocs\calc\main.php on line 24
が出力されました。
自分で試したこと
                echo Calculate::getInstance()
                    ->setType('-')
                    ->setInt1(10)
                    ->setInt2(51)
                    ->execute()
                    ->getResultValue()
                    .PHP_EOL;
では問題なく-41が出力されることを確認しております。
なにとぞよろしくお願いいたします。
0 likes
