Örnekteki gösterdiğim gibi çalışmasını istiyorum fakat nedense hata alıyorum.
Örnek dosyalarım:
Class.php
<?php
class Example
{
public $title;
function __construct()
{
$this->title = "title";
}
public function render()
{
require 'index.php';
}
}
$example = new Example();
$example->render();
?>İndex.php
<?php
echo $this->title;
?>
Class.php 'yi çalıştırdığımda dönen Sonuç Başarılı. $this->title ekrana bastırılıyor.
title
Normal olarak index.php'yi çalıştırdığımda dönen sonuç(normal sınıfımda alacağım sonuçta aynı olacak):
Fatal error: Using $this when not in object context in /var/www/orn/index.php on line 2
Fakat aşağıdaki kodlardaki gibi yapmaya çalıştığımda direkt index.php'yi çalıştırıyormuşum gibi bir hata ekrana bastırılıyor.
Gelen veriye göre dosyaları çağırdığım boot.class.php
<?php
class Boot {
function __construct() {
//$url = explode('/',$_GET['url']);
$url = $_GET['url'];
require 'call.class.php';
if(!isset($url))
{
$this->call = new Call('index');
}
elseif (isset($url))
{
$this->call = new Call('index'); // Veri geldiğinde çalışan kod burası.. <----
}
}
}
?>Template dosyalarını çağırdığım call.class.php 'de aşağıdaki gibi.
<?php
class Call{
function __construct($fileName) {
//$this->db = new Database;
$this->callFile($fileName);
}
function callFile($fileName)
{
$this->title = "title";
$this->description = "description";
$this->keywords = "keywords";
$fileName = $fileName . '.php';
require PAGESPATH.'common/header.php';
require PAGESPATH.$fileName;
require PAGESPATH.'common/footer.php';
}
}
?>Sorunun yaşandığı kısım ise burası header.php
<html>
<head>
<meta charset="utf-8">
<title><?php if(isset($this->title)){echo $this->title;} ?> - WebProgramlama.tk</title>
<meta name="description" content="<?php if(isset($this->description)){echo $this->description;}?>" />
<meta name="keywords" content="<?php if(isset($this->keywords)){echo $this->keywords;} ?>" />
</head>
<body>Ve PHP'nin ürettiği veri:
<html> <head> <meta charset="utf-8"> <title> Fatal error: Using $this when not in object context in /var/www/webprogramlama/class/pages/common/header.php on line 4
işte buradaki ayrımı yapamıyorum. Normalde sınıf içinde require ettiğim için $this-> ile değişkenlere ulaşmam lazım. Fakat direkt olarak header.php'yi çalıştırıyormuşum gibi hata alıyorum.
proje dosyalarının hepsi linkte:
http://db.tt/zINjoMR2