Lewes
Lewes

Reputation: 123

Variable seems to be uninitialized when defined

I'm trying to do an organised setup of a website, but I've ran into an issue, on the Class file, I've used $website['website_name'] = 'Delusion Servers'; yet when I place the variable inside a function, it says its uninitialized.

<?php

/*
 * Delusion Servers
 * Website access at (delusionservers.com).
 * Developed, designed and managed by Lewes B.
 * PAGE: WEBSITE.CLASS.PHP
 */

$website['website_name'] = 'Delusion Servers';

class website {

    function killPage($content){
        die("

            <h1>" . $website['website_name'] ."encountered an error</h1>

            <br /><br />" . $content . "

            ");
    }
}

$website = new Website;
?>

Whats wrong?

Upvotes: 0

Views: 2760

Answers (3)

Ibu
Ibu

Reputation: 43810

Solution 1: Class Constant

class Settings {
    const WEBSITE_NAME = 'Delusion Servers';
}

class website {

    function killPage($content){
        die("<h1>" . Settings::WEBSITE_NAME ." encountered an error</h1>
             <br /><br />" . $content);
    }
}

Solution 2: Global Constant

define("WEBSITE_NAME","Delusion Servers");
...
class website {

    function killPage($content){
        die("<h1>" . WEBSITE_NAME ." encountered an error</h1>
             <br /><br />" . $content);
    }
}

Solution 3 : Class property

class website {
    private $websiteName = "Delusion Servers";

    function killPage($content){
        die("<h1>" . $this->websiteName ." encountered an error</h1>
             <br /><br />" . $content);
    }
}

Upvotes: -1

Jay Bhatt
Jay Bhatt

Reputation: 5651

class website {

   public $website = array();

    public function __construct() {
        $this->website['website_name'] = 'Delusion Servers';
    }

    function killPage($content){
        die("

            <h1>" . $this->website['website_name'] ."encountered an error</h1>

            <br /><br />" . $content . "

            ");
    }
}

Upvotes: 1

Naftali
Naftali

Reputation: 146302

You function in the class is in a different scope.

Try this:

class website {

    function killPage($content){
        die("

            <h1>" . $this->website_name ."encountered an error</h1>

            <br /><br />" . $content . "

            ");
    }
}

$website = new Website;
$website->website_name = 'Delusion Servers';

Upvotes: 3

Related Questions