Bryce910 Posted February 25, 2012 Share Posted February 25, 2012 I am new to OOP php programming and I am trying to figure out what I did wrong on this. I am trying to call my protection() function in my users class, so I extended my question class and call the protection function but it isn't working. Here is my code for my class_lib: <?php require('dbc.php'); class users { public $email; public $password; public $first_name; public $last_name; public $id; function __construct($e,$p) { $this->email = $e; $this->password = $p; } function protection() { if(!$_COOKIE['user']) { header("location:../login.php"); } } } //new class class questions extends users { public $title; public $question; public $response; public $sub_category; public $date; public $pid; function __construct(){} function newest() { $sql = "SELECT * FROM math ORDER by pid DESC LIMIT 0,5"; $execute = mysql_query($sql) or die(mysql_error()); while($data = mysql_fetch_array($execute) ) { $this->title = $data['title']; $this->question = $data['question']; $this->pid = $data['pid']; echo '<p class="title">' . $this->title . '<br /></p>'; echo ' <br /><br /><br /><br /><a href="questions.php?id=' . $this->pid . '" class="button">Awnser it</a><br /><br /><br />'; } } } ?> Here is the page to call the function: <?php require('../class_lib.php'); $question = new questions(); $question -> protection(); ?> Quote Link to comment https://forums.phpfreaks.com/topic/257778-oop-extend-problem/ Share on other sites More sharing options...
batwimp Posted February 26, 2012 Share Posted February 26, 2012 Are you getting an error, or is it just doing nothing? If you are getting an error, post it here. If you aren't getting an error, it could be that $_COOKIE_['user'] is set, thus running the code that isn't there. You may want to try adding an else{} to that if statement to see if it's going there, just to test it. Something like: if(!$_COOKIE['user']) { header("location:../login.php"); }else{ echo "Cookie is set"; } Quote Link to comment https://forums.phpfreaks.com/topic/257778-oop-extend-problem/#findComment-1321333 Share on other sites More sharing options...
trq Posted February 26, 2012 Share Posted February 26, 2012 Before you go any further. Your *users* class should be named *user* is it obviously represents a single user. Extending that with *questions* make s little sense though I'm afraid. The two are in no way related. A *user* may own some *questions* but that doesn't meen you extend a user to contain questions. Quote Link to comment https://forums.phpfreaks.com/topic/257778-oop-extend-problem/#findComment-1321345 Share on other sites More sharing options...
Bryce910 Posted February 26, 2012 Author Share Posted February 26, 2012 There was no error on it at all. It also didn't run the else statement so I am not sure! Also so you don't think I should extend it but instead write another protection function in the questions class? Thansk Quote Link to comment https://forums.phpfreaks.com/topic/257778-oop-extend-problem/#findComment-1321440 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.