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

?>

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.