Jump to content

OOP extend problem.


Bryce910

Recommended Posts

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();
?>

Link to comment
Share on other sites

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";
                }

Link to comment
Share on other sites

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.

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.