简体   繁体   中英

PHP OOP Fatal error: Allowed memory size of 268435456 bytes exhausted

I wonder why my PHP has a fatal error.

It says:

Fatal error: Allowed memory size of 268435456 bytes exhausted (tried to allocate 130968 bytes)'

at the last echo 'geefOpslag' in the subclass Werknemer.

But I don't know why. I have a memory in my php.ini file of 128 MB.

<head>
    <meta charset="UTF-8">
    <title>Persoon</title>
</head>
<body>
    <?php

    class Persoon
    {

        public $naam;
        public $adres;
        public $email;

        public function __construct()
        {
            $this->naam = 'naam';
            $this->adres = 'adres';
            $this->email = 'email';
        }

        public function getNaw()
        {
            return $this->naam . $this->adres . $this->email;
        }

        public function setNaam($naam)
        {
            $this->naam = $naam;
        }

        public function setAdres($adres)
        {
            $this->adres = $adres;
        }

        public function setEmail($email)
        {
            $this->email = $email;
        }

    }

    $persoon = new Persoon();

    $persoon->setNaam("Harm ");
    $persoon->setAdres("Parkstraat 1 ");
    $persoon->setEmail("Harm@gmail.com");

    echo $persoon->getNaw();

    class Werknemer extends Persoon
    {

        public $datumInDienst;
        public $datumUitDienst;
        public $Salaris;
        public $opslag;

        function __construct()
        {
            parent::__construct();
            $this->datumInDienst = 'datumInDienst';
            $this->datumUitDienst = 'datumUitDienst';
            $this->Salaris = 'Salaris';
            $this->geefOpslag = 'geefOpslag';
        }

        public function setDatumInDienst($datumInDienst)
        {
            //return $this->datumInDienst;
            return $this->datumInDienst = $datumInDienst;
        }

        public function setDatumUitDienst($datumUitDienst)
        {
            //return $this->datumUitDienst;
            return $this->datumUitDienst = $datumUitDienst;
        }

        public function setSalaris($Salaris)
        {
            return $this->Salaris = $Salaris;
        }

        public function geefOpslag($geefOpslag)
        {
            return $this->geefOpslag($geefOpslag);
        }

    }

    $werknemer = new Werknemer();

    echo '<br/><br/>Datum in dienst: ', $werknemer->setDatumInDienst('13/12/2015');

    echo '<br/><br/>Datum uit dienst: ', $werknemer->setDatumUitDienst('13/12/2016');

    echo '<br/><br/>Salaris: ', $werknemer->setSalaris(1500);

    echo '<br/><br/>Opslag: ', $werknemer->geefOpslag(200);


    ?>


</body>

Your problem is with the method

public function geefOpslag($geefOpslag) {
    return $this->geefOpslag($geefOpslag);
}

When you call this method, it keeps calling on itself - and there's no stopping it. In effect, this causes an endless loop, which runs for so long that you use up all your memory. It would sort of be the same thing as running while (true); .

Your solution would be to have it return something, and not call on its own method again - it's logical that you should return the opfslag variable, making the return like this instead (of course you can do other things in here too, but calling it like it currently stands causes an endless loop, because there is no logic to stop it)

Perhaps what you're looking for is this instead

return $this->opslag = $geefOpslag;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM