Jump to content

how to use function within another function in a class?


salahassi

Recommended Posts

Hello guys!

 

I tried a lot but it didn't work... I'm working on an OOP project using PHP, and I'm stuck with kinda weird thing...

 

I have a class called Blocks works on defining the main menu and the blocks so I have made to functions the first one was for the main menu itself and the other one was for defining the blocks on right and left...

 

The thing is when I call the main menu in the other function which is "block" it gives me this fatal error:

 

Fatal error: Using $this when not in object context in D:\wamp\www\webtechTemplate\classes\blocks.php on line 39

 

I tried to use "self::MainMenu" and "$this->MainMenu" and "Blocks::MainMenu" and so on, but all those tries were unsuccessful :(

and when I erase the calling the error disappears..

 

some help please :'(

 

here is the whole code:

 

 

<?php
$filename = explode("/",$_SERVER['PHP_SELF']);
in_array("index.php", $filename) ? : die("You can't enter the page like this");//not to access the page directly

class Blocks{

var $menu= array(//defining menu items
                 'link1'=>'link1.php',//
                 'link2'=>'link2.php',//
                 'link3'=>'link3.php',//
                 'link4'=>'link4.php',//
                 'link5'=>'link5.php',//
                );


function block($dir, $menu1 = Null){// blocks list, they will be included from the database (they can be included from files as well)

    $mainMenu= self::MainMenu();
    if($menu1 == Null){
    if($dir=="left"){
        echo "left";
    }else{
        echo "right";
    }
    }else if($menu1=="menu"){
   
    if($dir=="left"){
        echo "left";
    }else{
        echo "right";
    }

    }

}

function MainMenu(){//menu defining
$menu = "<ul>";
    foreach($this->menu as $PageName => $pageLink){
$menu .= "<li><a href=\"$pageLink\">$PageName</a></li>";
    }
$menu .= "</ul>";

return $menu;

}

}// class end

?>

 

that doesn't really answer your question...

 

 

But I think you are looking at the wrong place where your getting the error, because when I copy that code it is in the class. are you using $this outside of the class somewhere accidentally?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.