Header(Location)

I’m having some small issues on trying to redirect a function to another function that’s inside the same “Class”

Here is the code for Admin.php

<?php

class Admin extends MainController{
	function __construct(){
			parent::__construct();
			$this->view->url	=	$this->config->url;
			$this->view->ID 	  = get_class($this);
			$this->view->Title = "iOwn Admin";
			$this->view->loginMessage = "";			
			if($_SESSION['user'] == ""){
				header("location:../Logout");
				exit(0);
			}else{
				
				$sql = "SELECT * FROM users WHERE username = :username ";
				$arr = array(":username" => $_SESSION['user']);
				$ctr = $this->database->DBCtr($sql,$arr);
				
				if($ctr > 0){
					$usr = $this->database->DBQry($sql,$arr);
					$this->view->usr = $usr[0]['username'];
					$role = $usr[0]['admin'];
				if($role == 1){
				header("location:../Admin");
				exit(0);
				}
			}	
		}
	}

	function AddAdmin(){
		$this->view->url	=	$this->config->url;
		$this->view->ID 	  = get_class($this);
		$this->view->Title = "iOwn > Admin > Add Admin";
		$this->view->render('Admin/AddAdmin');
	}	

}
?>
if($role == 1){
				header("location:../Admin");

Is it possible to make this redirect to

function AddAdmin

No.

The header(“Location:”) action redirects to another page, not a function.

If you want to access another function, you just simply have to call it.


function funone(){
     echo 'fun one!';
     funtwo();
}

function funtwo {
     echo 'fun two';
}

funone();


This is what I came up with from what you posted.

<?php

class Admin extends MainController{
	function __construct(){
			parent::__construct();
			$this->view->url	=	$this->config->url;
			$this->view->ID 	  = get_class($this);
			$this->view->Title = "iOwn Admin";
			$this->view->loginMessage = "";			
			if($_SESSION['user'] == ""){
				header("location:../Logout");
				exit(0);
			}else{
				
				$sql = "SELECT * FROM users WHERE username = :username ";
				$arr = array(":username" => $_SESSION['user']);
				$ctr = $this->database->DBCtr($sql,$arr);
				
				if($ctr > 0){
					$usr = $this->database->DBQry($sql,$arr);
					$this->view->usr = $usr[0]['username'];
					$role = $usr[0]['admin'];
				if($role == 1){
				AddAdmin();
				exit(0);
				}
			}	
		}
	}

	function AddAdmin(){
		$this->view->url	=	$this->config->url;
		$this->view->ID 	  = get_class($this);
		$this->view->Title = "iOwn > Admin > Add Admin";
		$this->view->render('Admin/AddAdmin');
	}	

}
?>

That returns an error. So i dont think i am quite understanding how you would call a function at the very beginning of a class.

If you’re calling object functions within an object, then you have to use self::thefunction() or parent::thefunction().

http://www.php.net/manual/en/language.oop5.paamayim-nekudotayim.php (see example #2 and #3)

My understanding was that self::thefunction() was for calling static methods and vars? For normal instance methods, you can call $this->thefunction() from within the object.

For variables, always use $this. $this is for retrieving variables within the instance of the object.

self:: or parent:: have more to do with scope within the class, rather than static vs non-static. Since functions aren’t variables or objects and instances don’t come into play, use self/parent. Self/parent simply refers to the current class.

When calling a function using self/parent, you can still call instance variables within that function using $this.

Not sure what you mean by this? You have to create a new instance of Admin for the constructor to be executed.

This would be the usual way of calling AddAdmin at the end of the constructor:


class Admin extends MainController
{
    function __construct(){
        // ..
        $this->AddAdmin();
    }

    function AddAdmin(){
        // ..
    }    
}

You create an instance of the class as an object in its entirety. However–functions don’t contain data–variables do.

Hence, you call an instance of a variable when you use $this.

However, to call a function from within the class, if you precede the call with self::, that indicates the scope of the call, which is inside of that class. If you’re calling a class that is inside of a class you extended, it is in the parent class, and you indicate that by calling parent::.

I wasn’t referring to instances of variables. My point was that $this refers to the current instance of a class, not to the class itself.

Although you can call the methods of a class from an instance using self::, there are situations where this might result in unwanted behaviour:


class Admin
{
    public function __construct()
    {
        // ..
        self::AddAdmin();
    }

    public function AddAdmin()
    {
        echo "Called Admin::AddAdmin";
    }
}

class BlogAdmin extends Admin
{
    public function AddAdmin()
    {
        echo "Called BlogAdmin::AddAdmin";
    }
}

$admin = new BlogAdmin;     // echos "Called Admin::AddAdmin"

Here, self continues to refer to the method as defined in the class where it’s defined (Admin), rather than the overridden method in BlogAdmin. If we’d used $this rather than self, the output would have been Called BlogAdmin::AddAdmin.