-2

I'm creating a system like MVC.
I'm using below class when I want to call view files. Edit: Meantime, He can do this: http://www.youtube.com/watch?feature=player_detailpage&v=Aw28-krO7ZM#t=1166s

<?php 
class Call{    
    function __construct($fileName) {
        //$this->db = new Database;
        self::callFile($fileName);
    }

    function callFile($fileName)
    {
        $this->title = "example";
        $this->description = "example";
        $this->keywords = "example";
        $fileName = $fileName . '.php';
        require PAGESPATH.'common/header.php';
        require PAGESPATH.$fileName;
        require PAGESPATH.'common/footer.php';
    }
}
?>

$fileName is index.php. Index.php has only

Index Page..

I want to print data in header.php like below:

<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>

But I'm getting errors.

Fatal error: Using $this when not in object context in /var/www/webprogramlama/class/pages/common/header.php on line 4

What can i solve this problem? Note: Please be careful! header.php is calling by Call class. header.php is inside of Call class.

Edit: And Why this is running?

tereško
  • 58,060
  • 25
  • 98
  • 150
Yusuf Ali
  • 29
  • 4

3 Answers3

3

Stop watching that stupid "tutorial". It is so bad that at one point i actually begun to laugh.

You cannot just pick up a language, without any previous experience, and just start using high-level concepts. MVC is one of such concepts. To actually grasp it you need to understand object oriented programming and a lot of principles, that are associated with it.

.. etc. And you wont understand those principles just by reading the articles.

As for how to solve your problem, you could read this article. It will explain how to use templates. Which is actually what your "mvc tutorial" there actually is - bad guide for making routing mechanism and templates.

Also, you must understand that, if you do self::something();, it is outside an object. You are calling a static method on a class, which actually is just poor way of doing procedural programming.

You should start by learning the basics of OOP in PHP, because you don't get it. And for your own good, stay away from MVC and frameworks for a year at least.

tereško
  • 58,060
  • 25
  • 98
  • 150
2

Instead of calling the elements as $this->title outside the class, you need to create a new instance of the object in your code this like:

$callObject= new call($filename);

then refer to it in the page like this:

$callObject->title;

You can only use the $this-> code inside the class itself. Anywhere else, you need to make an object and therefore $this doesn't exist - the object of that class does - and you then have to refer to it by its name. At the same time, if your variable is going to be called $callObject you can't refer to it like that inside the class - as at that point you haven't made an instance of it, so you need to refer to it via the $this syntax which is a nice way of saying my element called title.

Edit: okay, I see what you are doing now, goodness me.

That's rather dangerous because your header.php file will contain loads of things that will only work inside the class called class - and generate hideous errors in every single other circumstance.

PHP will let you have the header.php file as it is, but as PHP evaluates the contents as it goes, your object is incomplete. You should have a read of this question which will answer that part in more detail.

Edit 2: Don't split a function between files.

If the code inside header.php is written be only used within the function (as it seems to be) copy the entire contents of it inside the class - and don't use a require or include to try to paste it in on the fly.

Never have more than one file for a class. It doesn't matter how long the file is.

Edit 3:

This is what your code should look like for the header section:

<?php 
class Call{    
    function __construct($fileName) {
    //$this->db = new Database;
    self::callFile($fileName);
    }

    function callFile($fileName)
    {
    $this->title = "example";
    $this->description = "example";
    $this->keywords = "example";
    $fileName = $fileName . '.php';
    echo "
<html>
    <head>
    <meta charset='utf-8'>
    <title>        
    ";
    echo (isset($this->title)) ? $this->title : "";
    echo "
 - WebProgramlama.tk</title>
    <meta name='description' content='
    ";
    echo (isset($this->description)) ? $this->description : "";
    echo "' />
    <meta name='keywords' content='
    ";
    echo (isset($this->contents)) ? $this->contents : "";
    echo "
' />
   </head>
   <body>
    ";

    //require PAGESPATH.$fileName;
    //require PAGESPATH.'common/footer.php';
    // you can only include files that don't use any $this-> type elements.
    }
}
?>
Community
  • 1
  • 1
Fluffeh
  • 33,228
  • 16
  • 67
  • 80
1

$this has no context outside of the class. Create an instance of your class and use that instead.

<html>
    <head>
        <meta charset="utf-8">
<?php
    $class = new Call('filename');
?>    
    <title><?php if(isset($class->title)){echo $class->title;} ?> - WebProgramlama.tk</title>
    <meta name="description" content="<?php if(isset($class->description)){echo $class->description;}?>" />
    <meta name="keywords" content="<?php if(isset($class->keywords)){echo $class->keywords;} ?>" />
   </head>
   <body>

Get rid of your require statements in your callFile function; they have no place being there.

wanovak
  • 6,117
  • 25
  • 32