Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

PHP Inheritance Issue

Featured Replies

My question (or problem) exists with trying to find if a child class has access to a method of a certain name.

 

I've done some google searching on it, and one solution I've found is simply passing the instance of the child class to the parent but I was wondering if there was a way to check in a more direct manner.

 

<?

abstract class ParentClass {

   protected function __construct( $var=null ) {
       if ($var == "testing") {
           // 1. check if child has method "testing"
           // 2. call "testing"
       }
   }

}

class ChildClass extends ParentClass {

   public function __construct( $var=null ) {
       parent::__construct( $var );
   }

   public function testing() {
       echo "Testing exists";
   }

}

$child = new ChildClass("testing");

Edited by Siege84

  • Author

you can check method_exists and is_callable. Remember that parent methods and members will be available to the child class so you can probably get away with using $this or parent.

 

If you need more advanced checking your can use reflection - http://php.net/manua....reflection.php

 

I'm not at all familiar with reflection yet. However, after seeing your post I adjusted my code to the following and it seems to work as intended. Just a small change to the parent constructor:

 

<?

abstract class ParentClass {

   protected function __construct( $var=null ) {
       if ( method_exists($this, $var) && is_callable( array($this, $var)) )
           $this->$var();
   }

}

class ChildClass extends ParentClass {

   public function __construct( $var=null ) {
       parent::__construct( $var );
   }

   public function testing() {
       echo "Testing exists";
   }

}

$one = new ChildClass();
$two = new ChildClass("testing");

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.