problem when printing class's variables

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Derpost
    New Member
    • Aug 2012
    • 1

    #1

    problem when printing class's variables

    I'm creating a system like MVC.
    I'm using below class when I want to call view files.

    Code:
    <?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

    Code:
    Index Page..
    I want to print data in header.php like below:
    Code:
    <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.
    Code:
    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.
  • Claus Mygind
    Contributor
    • Mar 2008
    • 571

    #2
    Code:
        <?php 
        class Call{ 
    //declare your properties first
                public $title;
                public $description;
                public $keywords;
    
       
            function __construct($fileName) {
                //$this->db = new Database;
                self::callFile($fileName);
            }
         
            function callFile($fileName)
            {
    //then you should be able to use them in your methods later       
                $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';
            }
        }
        ?>
    you may even learn more at this link http://www.php.net/manual/en/language.oop5.basic.php

    here is the php manual example


    <?php
    class SimpleClass
    {
    // property declaration
    public $var = 'a default value';

    // method declaration
    public function displayVar() {
    echo $this->var;
    }
    }
    ?>

    Comment

    Working...