salahassi Posted June 3, 2011 Share Posted June 3, 2011 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 https://forums.phpfreaks.com/topic/238344-how-to-use-function-within-another-function-in-a-class/ Share on other sites More sharing options...
fugix Posted June 3, 2011 Share Posted June 3, 2011 fist time ive seen code written in url format...lol can you change it please Link to comment https://forums.phpfreaks.com/topic/238344-how-to-use-function-within-another-function-in-a-class/#findComment-1224853 Share on other sites More sharing options...
salahassi Posted June 3, 2011 Author Share Posted June 3, 2011 hehe sorry, it was a mistake! Link to comment https://forums.phpfreaks.com/topic/238344-how-to-use-function-within-another-function-in-a-class/#findComment-1224854 Share on other sites More sharing options...
The Little Guy Posted June 3, 2011 Share Posted June 3, 2011 you should be able to do this: $mainMenu= $this->MainMenu(); But when you look in the function that does that, you never use $mainMenu again. Link to comment https://forums.phpfreaks.com/topic/238344-how-to-use-function-within-another-function-in-a-class/#findComment-1224860 Share on other sites More sharing options...
The Little Guy Posted June 3, 2011 Share Posted June 3, 2011 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? Link to comment https://forums.phpfreaks.com/topic/238344-how-to-use-function-within-another-function-in-a-class/#findComment-1224865 Share on other sites More sharing options...
salahassi Posted June 3, 2011 Author Share Posted June 3, 2011 No I don't, I checked that out.. isn't that weird? I called the functions separately and they worked, and this error occurs only with this class I don't know why!! Link to comment https://forums.phpfreaks.com/topic/238344-how-to-use-function-within-another-function-in-a-class/#findComment-1224875 Share on other sites More sharing options...
salahassi Posted June 3, 2011 Author Share Posted June 3, 2011 Help help help please :'( (I feel like a baby LOL) Link to comment https://forums.phpfreaks.com/topic/238344-how-to-use-function-within-another-function-in-a-class/#findComment-1224883 Share on other sites More sharing options...
fugix Posted June 3, 2011 Share Posted June 3, 2011 you can separate the functions using different classes for each....then call the function in your blocks class like.... classname::MainMenu(); Link to comment https://forums.phpfreaks.com/topic/238344-how-to-use-function-within-another-function-in-a-class/#findComment-1224885 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.