SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Member
    Join Date
    Jul 2013
    Posts
    16
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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

    Code:
    <?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');
    	}	
    
    }
    ?>
    Code:
    if($role == 1){
    				header("location:../Admin");
    Is it possible to make this redirect to
    Code:
    function AddAdmin

  2. #2
    Barefoot on the Moon! silver trophy Force Flow's Avatar
    Join Date
    Jul 2003
    Location
    Northeastern USA
    Posts
    4,606
    Mentioned
    56 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by Hulu View Post
    Is it possible to make this redirect to
    Code:
    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.


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

    function 
    funtwo {
         echo 
    'fun two';
    }

    funone(); 
    Visit The Blog | Follow On Twitter
    301tool 1.1.5 - URL redirector & shortener (PHP/MySQL)
    Can be hosted on and utilize your own domain

  3. #3
    SitePoint Member
    Join Date
    Jul 2013
    Posts
    16
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Force Flow View Post
    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.


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

    function 
    funtwo {
         echo 
    'fun two';
    }

    funone(); 
    This is what I came up with from what you posted.

    Code:
    <?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.

  4. #4
    Barefoot on the Moon! silver trophy Force Flow's Avatar
    Join Date
    Jul 2003
    Location
    Northeastern USA
    Posts
    4,606
    Mentioned
    56 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by Hulu View Post
    This is what I came up with from what you posted.

    Code:
    <?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/languag...ekudotayim.php (see example #2 and #3)
    Visit The Blog | Follow On Twitter
    301tool 1.1.5 - URL redirector & shortener (PHP/MySQL)
    Can be hosted on and utilize your own domain

  5. #5
    Community Advisor bronze trophy
    fretburner's Avatar
    Join Date
    Apr 2013
    Location
    Brazil
    Posts
    1,397
    Mentioned
    45 Post(s)
    Tagged
    12 Thread(s)
    Quote Originally Posted by Force Flow View Post
    If you're calling object functions within an object, then you have to use self::thefunction() or parent::thefunction().
    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.

  6. #6
    Barefoot on the Moon! silver trophy Force Flow's Avatar
    Join Date
    Jul 2003
    Location
    Northeastern USA
    Posts
    4,606
    Mentioned
    56 Post(s)
    Tagged
    1 Thread(s)
    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.
    Visit The Blog | Follow On Twitter
    301tool 1.1.5 - URL redirector & shortener (PHP/MySQL)
    Can be hosted on and utilize your own domain

  7. #7
    Community Advisor bronze trophy
    fretburner's Avatar
    Join Date
    Apr 2013
    Location
    Brazil
    Posts
    1,397
    Mentioned
    45 Post(s)
    Tagged
    12 Thread(s)
    Quote Originally Posted by Force Flow View Post
    Since functions aren't variables or objects and instances don't come into play, use self/parent.
    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:
    PHP Code:
    class Admin extends MainController
    {
        function 
    __construct(){
            
    // ..
            
    $this->AddAdmin();
        }

        function 
    AddAdmin(){
            
    // ..
        
    }    


  8. #8
    Barefoot on the Moon! silver trophy Force Flow's Avatar
    Join Date
    Jul 2003
    Location
    Northeastern USA
    Posts
    4,606
    Mentioned
    56 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by fretburner View Post
    Not sure what you mean by this? You have to create a new instance of Admin for the constructor to be executed.
    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::.
    Visit The Blog | Follow On Twitter
    301tool 1.1.5 - URL redirector & shortener (PHP/MySQL)
    Can be hosted on and utilize your own domain

  9. #9
    Community Advisor bronze trophy
    fretburner's Avatar
    Join Date
    Apr 2013
    Location
    Brazil
    Posts
    1,397
    Mentioned
    45 Post(s)
    Tagged
    12 Thread(s)
    Quote Originally Posted by Force Flow View Post
    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.
    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.

    Quote Originally Posted by Force Flow View Post
    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::.
    Although you can call the methods of a class from an instance using self::, there are situations where this might result in unwanted behaviour:
    PHP Code:
    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.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •