课堂上的私人变量不显示

时间:2015-11-10 09:40:55

标签: php class oop magic-methods

所以我有一段代码,不知何故不起作用。我真的很反对它,因为我之前已经多次使用它,我真的不知道问题是什么。

我已经使用语言变量设置了一个配置类,并在构造函数中设置了这些变量。如果我在他们工作的构造函数中使用它们,但是当我尝试从继承配置类的其他类中的构造函数外部调用它们时,它们显示为空。这是我的代码:

    <?php
// Start de sessie
ob_start();
session_start();

// Locate application path
define('DS', DIRECTORY_SEPARATOR);
define('ROOT', dirname(dirname(__FILE__)));

function __autoload($className) {
    // Add classes
    if (file_exists(ROOT . DS .'classes'. DS . ucfirst(strtolower($className)) . '.php')) {
        require_once (ROOT . DS .'classes'. DS . ucfirst(strtolower($className)) . '.php');
    } else {
        die('Class not found ('.$className.')');
    }
}


if(!isset($_SESSION['lang'])){
    $_SESSION['lang'] = 'nl';
}
if(isset($_GET['lang'])) {
    $lang = $_GET['lang'];
    if($lang != 'en' && $lang != 'nl') {
        $_SESSION['lang'] = 'nl';
    }else {
        $_SESSION['lang'] = $lang;
    }
}
class Config {

    //declare variables
    private $kmailtext;
    private $kslidertext;
    private $kcontactbutton;
    private $kofferstitle;
    private $kofferssecondtitle;
    private $khomebanner1;
    private $khomebanner2;
    private $khomebanner3;
    private $khomebanner4;
    private $kmaintenancetitle;
    private $kmaintenancetitle1;
    private $kmaintenancetitle2;

    function __construct() {
        if($_SESSION['lang'] == 'nl') {
            //    NEDERLANSE TEXT
            $this->kmailtext = "Mail ons";
            $this->kslidertext = "Karsten Installatie helpt u van A tot Z";
            $this->kcontactbutton = "contact";
            $this->kofferstitle = "CV-KETELS";
            $this->kofferssecondtitle = "Bekijk onze aanbiedingen uit onze selectie van CV-Ketels";
            $this->khomebanner1 = "CV Installatie";
            $this->khomebanner2 = "CV Onderhoud";
            $this->khomebanner3 = "Loodgieter";
            $this->khomebanner4 = "Advies";
            $this->kmaintenancetitle = "ONDERHOUDSCONTRACTEN";
            $this->kmaintenancetitle1 = "Onderhoudscontracten";
            $this->kmaintenancetitle2 = "Kiest u voor een onderhoudscontract, dan ben u zeker van deskundig onderhoud van uw cv-ketel en/of warmwaterinstallatie.";
        } else {
        //    ENGELSE VERTALING
            $this->kmailtext = "Mail us";
            $this->kslidertext = "Karsten Installatie helps u from A to Z";
            $this->kcontactbutton = "contact";
            $this->kofferstitle = "Boilers";
            $this->kofferssecondtitle = "Check our offers from our boiler selection";
            $this->khomebanner1 = "Boiler installation";
            $this->khomebanner2 = "Boiler maintenance";
            $this->khomebanner3 = "Plumber";
            $this->khomebanner4 = "Advice";
            $this->kmaintenancetitle = "MAINTENANCE CONTRACTS";
            $this->kmaintenancetitle1 = "Maintenance Contracts";
            print_r($this->kmaintenancetitle);
        }
    }

    public function __get($name) {
        return $this->$name;
    }

}

class someOtherClass extends Config {

     public function someFunction() {
         return $this->kmaintenancetitle;
     }

}

$someclass = someOtherClass();
echo $someclass->someFunction();
?>

3 个答案:

答案 0 :(得分:2)

$config = new Config();
echo $this->kmaintenancetitle;

应该是

$config = new Config();
echo $config->kmaintenancetitle;

然后它会成功调用魔法吸气剂

答案 1 :(得分:1)

您刚刚忘记了此行中的new

$someclass = someOtherClass();

应该是

$someclass = new someOtherClass();

然后一切正常。虽然这仅适用于您问题的第二个版本,但不适用于第一个版本!

答案 2 :(得分:0)

将配置类属性声明为受保护。

class Config {

//declare variables    
...
protected $kmaintenancetitle;
protected $kmaintenancetitle1;
protected $kmaintenancetitle2;