php未定义变量/未检测类

时间:2013-10-11 10:44:08

标签: php html pdo

我正在为一些有趣的项目工作,但我现在卡住了。每次我调用我的index.php时都会收到通知和致命错误。

Notice: Undefined variable: translator in C:\xampp\htdocs\Curve\manage\pages\menu.sfwp on line 309

Fatal error: Call to a member function getText() on a non-object in C:\xampp\htdocs\Curve\manage\pages\menu.sfwp on line 309

我需要所有文件并打电话给我的班级。

结构: index.php - > core / core.inc.php - >类/ Translator.cs.php index.php - >页/ menu.sfwp

核心:

if(corekey != "STR456512013213280935405CMS"){
    die();
}
//Sessionn
session_start();

//MySQL Connection
try{
    $conn = new PDO("mysql:host=localhost;dbname=Curve", 'root', '');
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(PDOException $Exception){
    throw new Exception( $Exception->getMessage( ) , $Exception->getCode( ) );
}

//classes
require('classes/General.cs.php');
require('classes/Users.cs.php');
require('classes/Portal.cs.php');
require('classes/Translation.cs.php');

$users          = new users($conn);
$general        = new general($conn);
$portal         = new portal($conn);
$templatePortal = new portalTemplate($conn);
$translator     = new Translator($conn);

Menu.sfwp

<?php $translator->getTxt(1, $_SESSION['lang']); ?> 

translator.cs.php

class Translator
{
    private $db;

    public function __construct($database) {
        $this->db = $database;
    }

    private function getTxt($text, $userlang){
        $lang = "lang_".$userlang;
        $query = $this->db->prepare("SELECT * FROM ".$lang." WHERE id='".$text."'");
        $query->execute();
        $row = $query->fetch();
        echo($row->text);
    }
}

3 个答案:

答案 0 :(得分:0)

在Menu.sfwp中使用require_once('classes/Translation.cs.php');

<?php 
$translator=new Translator($conn);

$translator->getText(1, $_SESSION['lang']); 

?> 

答案 1 :(得分:0)

Menu.sfwp中,您试图调用不存在的getText方法 您的Translator课程改为使用getTxt方法 这是一个错字。

您的Menu.wfwp应如下所示:

<?php $translator->getTxt(1, $_SESSION['lang']); ?> 

答案 2 :(得分:0)

你的Menu.sfwp应该有

<?php 
   $translator     = new Translator($conn);
   $translator->getText(1, $_SESSION['lang']);
?> 

同时检查getTxt的拼写。