Fatal error and use of undefined constant in child theme unless I redefine it


Child theme functions.php:

// Init. require_once( WPBF_CHILD_THEME_DIR . '/inc/init-child.php'); 

In the parent theme functions.php, the constant is defined:

define( 'WPBF_CHILD_THEME_DIR', get_stylesheet_directory() ); 

If I include the same constant definition in the child theme like the above, then the require_once works and /inc/init-child.php file is loaded.

However I get a notice stating that the constant is already defined in the parent theme’s functions.php (correct!).

But if I remove this from the child theme’s functions.php, then I get the following:-

Warning: Use of undefined constant WPBF_CHILD_THEME_DIR - assumed 'WPBF_CHILD_THEME_DIR' (this will throw an Error in a future version of PHP) in /var/www/vhosts/xxx/subdomains/xxx/httpdocs/wp-content/themes/page-builder-framework-child/functions.php on line 42  Warning: require_once(WPBF_CHILD_THEME_DIR/inc/init-child.php): failed to open stream: No such file or directory in /var/www/vhosts/xxx/subdomains/xxx/httpdocs/wp-content/themes/page-builder-framework-child/functions.php on line 42  Fatal error: require_once(): Failed opening required 'WPBF_CHILD_THEME_DIR/inc/init-child.php' (include_path='.:/opt/plesk/php/7.3/share/pear') in /var/www/vhosts/xxx/subdomains/xxx/httpdocs/wp-content/themes/page-builder-framework-child/functions.php on line 42 

So why can I only load the file via the child theme if I redefine the constant even though it is going to show me a notice to state it is already defined…?

The same constant is already in use in the very same child theme functions.php file further up as well like:

// Textdomain. load_child_theme_textdomain( 'page-builder-framework-child', WPBF_CHILD_THEME_DIR . '/languages' ); 

I am assuming there should be a correct way to load the file needed without having to repeat constant definitions?