2

I have a static View class that gets passed a string from another class. When the string is passed through as a variable it works. When I change it to a constant the error is:

[17-Feb-2016 19:08:48 Europe/Berlin] PHP Warning: include(): Failed opening '/Applications/MAMP/htdocs/its_vegan/scripts/back_end/views/template' for inclusion (include_path='.:/Applications/MAMP/bin/php/php7.0.0/lib/php') in /Applications/MAMP/htdocs/its_vegan/scripts/back_end/views/view.php on line 23

class View {

    /**
     * -------------------------------------
     * Render a Template.
     * -------------------------------------
     * 
     * @param $filePath - include path to the template.
     * @param null $viewData - any data to be used within the template.
     * @return string - 
     * 
     */
    public static function render( $filePath, $viewData = null ) {

        // Was any data sent through?
        ( $viewData ) ? extract( $viewData ) : null;

        ob_start();
        include ( $filePath );// error on this line
        $template = ob_get_contents();
        ob_end_clean();

        return $template;
    }
}

class CountrySelect {

    const template = 'select_template.php'; //the const is template

    public static function display() {

        if ( class_exists( 'View' ) ) {

            // Get the full path to the template file.

            $templatePath = dirname( __FILE__ ) . '/' . template; //the const is template

            $viewData = array(
                "options" => '_countries',
                "optionsText" => 'name',
                "optionsValue" => 'geonameId',
                "value" => 'selectedCountry',
                "caption" => 'Country'
            );

            // Return the rendered HTML
            return View::render( $templatePath, $viewData );

        }
        else {
            return "You are trying to render a template, but we can't find the View Class";
        }
    }
}

What did work was having this in the CountrySelect:

$templatePath = dirname( __FILE__ ) . '/' . static::$template;

Why does the template have to be static? Can I make it a static constant?

2
  • You need to refer to it using self::constname Commented Feb 17, 2016 at 18:30
  • Access to class constants done via the double-colon :: operator. (AKA Scope Resolution Operator). If the static keyword is bothering you then an alternative is self. The docs can shed some light on the topic. Commented Feb 17, 2016 at 18:32

2 Answers 2

4

You could also use self::template
Since class constants are defined at a per class level instead of per-object, static::template will refer to the same, unless you have a child class. (See https://secure.php.net/manual/en/language.oop5.late-static-bindings.php)

template refers to a global constant (e.g. by define('template', 'value');)

Sign up to request clarification or add additional context in comments.

Comments

2

At this line

$templatePath = dirname( __FILE__ ) . '/' . template; 

template is not constant, because constant template declared inside class. This code works similar

$templatePath = dirname( __FILE__ ) . '/template'; 

so, use static::template

2 Comments

Not my downvote, but OP's code shows as const template = 'select_template.php'; //the const is template and seems out of scope.
whoever downvoted, should now retract. It'd be the honorable thing to do.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.