Fatal error: Cannot access parent:: when current class scope has no parent
Fatal error: Uncaught Error: Cannot access parent:: when current class scope has no parentShort answer
PHP hit a parent:: call inside a class that does not extend anything. The call is wrong for that class: either the extends clause is missing, or the code was copied from a child class. Add the right extends, or delete the parent:: call. In a trait, the using class must extend a parent.
Fix it fast
1Find out which cause you have
Read the file and line in the error, then list the class declaration and every parent:: use in that file. The class that holds the parent:: call must have an extends clause.
grep -n -E "^\s*(abstract |final )?(class|trait) |parent::" src/Service/Mailer.php2Match what you see, then jump to its fix
- 1
The failing line is parent::__construct() or parent::someMethod() inside a class declared as 'class Name' with nothing after itThe class has no extends clauseFix - 2
The failing line is inside a trait file, and the class that uses the trait has no extendsA trait calls parent:: but the class that uses it has no parentFix
- 1
What does the error look like?
Fatal error: Uncaught Error: Cannot access parent:: when current class scope has no parentDeprecated: Cannot use "parent" when current class scope has no parent
Cause 1 of 2
The class has no extends clause
1Check
Look at the class declaration. The manual says parent works from inside a class definition, and its examples always use it in a class that extends another one. In a class without a parent there is nothing to point at. The manual says the error is generated when the parent is accessed at run time.
grep -n "class Mailer" src/Service/Mailer.php
php -l src/Service/Mailer.php2Fix
If the class should inherit from a base class, add extends. If it should not, remove the parent:: call (a constructor with no parent needs no parent::__construct()).
// before
class Mailer
{
public function __construct() { parent::__construct(); }
}
// after: the class really has a base class
class Mailer extends BaseService
{
public function __construct() { parent::__construct(); }
}3Verify
Run the code path that creates the object. The error no longer appears.
php -r 'require "vendor/autoload.php"; new App\Service\Mailer();'Confirmed by php.net
Cause 2 of 2
A trait calls parent:: but the class that uses it has no parent
1Check
Trait methods are resolved against the class that uses the trait. The manual's trait example calls parent::sayHello() from a trait and works only because the using class extends Base. Find which classes use the trait.
grep -rn "use LogsActivity;" src/
grep -rn "trait LogsActivity" src/2Fix
Make every class that uses the trait extend a class that has the method, or change the trait so it does not call parent::.
trait LogsActivity
{
public function save(): void
{
// runs the using class's parent method; the class must extend one
parent::save();
// log here
}
}
class Order extends Model
{
use LogsActivity;
}3Verify
Call the trait method on an object of each class that uses it.
php -r 'require "vendor/autoload.php"; (new App\Order())->save();'Confirmed by php.net
How do I stop it coming back?
- Run php -l and a static analyser (PHPStan or Psalm) in CI: they flag parent:: in a class without a parent before it runs.
- When you copy a method from a child class, check whether the new class extends the same base.
- Prefer composition (a property holding the other object) over a trait that depends on parent:: methods.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
Related fixes
Back end
Android gets HTML instead of JSON from a PHP API
org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject
The response body is not JSON.
2 causes, each with a fixBack end
Apache returns 403 when a URL contains %3F
a request like /page%3Fname=test is refused while /page?name=test works
Recent Apache httpd blocks a rewrite when the request contains an encoded question mark (%3F) and the rewritten target has a literal ?.
2 causes, each with a fixBack end
Apache returns 500 with Premature end of script headers
500 Internal Server Error
Apache ran your script, but the script printed no valid HTTP header before its output.
3 causes, each with a fixBack end
Apache returns 503 or 504 because PHP-FPM did not answer in time
Error dispatching request
Apache sent the request to PHP-FPM and gave up waiting.
3 causes, each with a fixBack end
AWS SDK for PHP cannot get credentials from the instance profile metadata server
Error retrieving credentials from the instance profile metadata server.
The SDK tries the EC2 instance metadata service when it has no valid credentials from anywhere earlier in its chain.
4 causes, each with a fixBack end
AWS SDK for PHP rejects requests: the signature is from the future
Signature not yet current: {date} is still later than {date}
AWS compares the timestamp in your signed request with its own clock.
2 causes, each with a fix