Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    Regular Coder
    Join Date
    Jul 2002
    Posts
    165
    Thanks
    0
    Thanked 0 Times in 0 Posts

    assigned an object to session in class and reloading, stupid or possible?

    Code:
    <?
    session_start();
    header("Cache-control: private");
    ?>
    <?php include("pageControl.php") ?>
    
    <?php
    # baseclass to test ability to grab the session objects
    $pageInitObj = new pageInitialisation();
    
    class pageInitialisation
    {
    	var $pageDefObj;
    	
    	function pageInitialisation()
    	{
    		print "<br><b>New instantiation of pageInitialisation</b>";
    		print "<br><br>session has? - " . $_SESSION["pageDefObj"];
    
    		if(isset($_SESSION["pageDefObj"]))
    		{
    			print "<br>session has object of type pageDefObj so setting pageInitialisation pageDefObj to session obj";
    			
    			# if exists, get ref to and so become
    			$this->$pageDefObj =& $_SESSION["pageDefObj"];
    			$this->$pageDefObj->printSomething();
    		}
    		else
    		{
    			print "<br>session for obj pageDefObj is empty";
    			# create new obj of type pageControl, then assign to session obj
    			print "<br>Setting pageDefObj now<br>";
    			
    			$this->pageDefObj = new pageControl();
    			
    			$_SESSION["pageDefObj"] =& $this->$pageDefObj;
    			print "<br>added obj to session";
    		}
    	}
    }
    
    ?>
    Hiya, I am trying to work out if the global $_SESSION variable can be used as I've tried to above.

    I want to load one class, then reload an object from the session and assign it to a variable in that class (which is a reference to another class)

    Essentially meaning one class call is all thats needed to setup the page, work out details, do db loads (or use already loaded) etc...

    Thx for comments

  • #2
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,040
    Thanks
    10
    Thanked 92 Times in 90 Posts
    yes , as long as the class definition is available (eg included ) thats fine ... e.g

    PHP Code:
    <?
    session_start
    ();
    include_once 
    'bar.class.php';

    class 
    foo{
        function 
    foo(){
            
    $this->bar = ( isset( $_SESSION['bar'] ) ) ? $_SESSION['bar'] : new bar() ;
            
    print_r$this->bar );
        }
    }

    foo::foo();
    ?>
    where $_SESSION['bar'] is an instance of class bar() from bar.class.php;
    Last edited by firepages; 11-21-2003 at 06:47 PM.
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

  • #3
    Regular Coder
    Join Date
    Jul 2002
    Posts
    165
    Thanks
    0
    Thanked 0 Times in 0 Posts

    ok, tried a few examples but seem to be stuck

    Code:
    <?php
    	
    class pageInitialisation
    {
    	var $pageDefObj;
    	
    	function pageInitialisation()
    	{
    		print "<br><b>New instantiation of pageInitialisation</b>";
    		print "<br><br>session has? - " . $_SESSION["pageDefObj"];
    
    		$this->pageDefObj = ( isset( $_SESSION['pageDefObj'] ) ) ? $_SESSION['pageDefObj'] : $this->pageDefObj =& new pageControl();
    
    		if(isset($_SESSION["pageDefObj"]))
    		{
    			print "<br>session has object of type pageDefObj so setting pageInitialisation pageDefObj to session obj";
    			
    			# if exists, get ref to and so become
    			#$this->pageDefObj =& $_SESSION["pageDefObj"];
    			$this->pageDefObj->printSomething();
    		}
    		else
    		{
    			print "<br>session for obj pageDefObj is empty";
    			# create new obj of type pageControl, then assign to session obj
    			print "<br>Setting pageDefObj now<br>";
    			
    			#$this->pageDefObj = new pageControl();
    			
    			#$_SESSION["pageDefObj"] =& $this->$pageDefObj;
    			print "<br>added obj to session";
    		}
    		
    		$_SESSION['pageDefObj'] =& $this->pageDefObj;
    		
    	}
    	
    }
    
    # baseclass to test ability to grab the session objects
    #$pageInitObj = new pageInitialisation();
    pageInitialisation::pageInitialisation();
    ?>
    What am i doing wrong? The page I am using has 3 buttons (in form) directed to index.php?id=1/2 & 3, but once clicked, I get error executing function as if the object doesnt really exist...?

    Thx for comments

    p.s. that function if you need to know, is the call to $this->pageDefObj->printSomething();

  • #4
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,040
    Thanks
    10
    Thanked 92 Times in 90 Posts
    Hi , try this as a diagnostic , without having the included classes is hard for me to guess whats happening ..but often dereferencing the object can help ... eg

    $obj = $this->object;
    $obj->do_something();

    see what this outputs .. if the method call does not work it should dump the object

    PHP Code:
    <?php
    class pageInitialisation{
        var 
    $pageDefObj;
        function 
    pageInitialisation(){
            print 
    "<br><b>New instantiation of pageInitialisation</b>";
            print 
    "<br><br>session has? - " $_SESSION["pageDefObj"];

            
    $this->pageDefObj = ( isset( $_SESSION['pageDefObj'] ) ) ? $_SESSION['pageDefObj'] : new pageControl() ;

            
    $obj=$this->pageDefObj;

            if( 
    method_exists$obj ,'printSomething' ) ){
                
    $obj->printSomething();
            }else{
                echo 
    '<pre>';
                
    print_rget_class_methods$obj ) );
                
    var_export$obj ) ;
                echo 
    '</pre>';
            }

            
    $_SESSION['pageDefObj'] =& $this->pageDefObj;
        }
    }
    pageInitialisation::pageInitialisation();
    ?>
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

  • #5
    Regular Coder
    Join Date
    Jul 2002
    Posts
    165
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hiya, ran the code and outputted:

    Code:
    Array
    (
    )
    array (
      '__PHP_Incomplete_Class_Name' => 'pagecontrol',
    )
    Code:
    class pageControl
    {
    	
    	function pageControl()
    	{
    		print "<br><b>PAGECONTROL - New instantiation of pageControl</b>";
    	}
    	
    	function printSomething()
    	{
    		print "<br>Something printed<br>";
    	}
    }
    The pageControl class is only a test atm, but later on tonight I'll be building it (its in my self-employed state which == after work)

    Thx for comments

  • #6
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,040
    Thanks
    10
    Thanked 92 Times in 90 Posts
    Hi , the error you post is what I would expect when running the code above without having the pageControl class available , eg (and I forgot to put this in my example so apologies if this is the issue..)

    <?php
    include ('pageControl.class.php');

    //rest of code
    ?>

    eg without the class definition the object can not be reconstructed.
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

  • #7
    Regular Coder
    Join Date
    Jul 2002
    Posts
    165
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hi, well, heres the whole page including my commented out class, the definition is definetely included, I was wondering if it was something to do with passing a reference to the object into the session

    Code:
    <?
    session_start();
    header("Cache-control: private");
    ?>
    <?php include("pageControl.php") ?>
    
    <?php
    	/*
    class pageInitialisation
    {
    	var $pageDefObj;
    	
    	function pageInitialisation()
    	{
    		print "<br><b>New instantiation of pageInitialisation</b>";
    		print "<br><br>session has? - " . $_SESSION["pageDefObj"];
    
    		$this->pageDefObj = ( isset( $_SESSION['pageDefObj'] ) ) ? $_SESSION['pageDefObj'] : $this->$pageDefObj =& new pageControl();
    
    		if(isset($_SESSION["pageDefObj"]))
    		{
    			print "<br>session has object of type pageDefObj so setting pageInitialisation pageDefObj to session obj";
    			
    			# if exists, get ref to and so become
    			#$this->pageDefObj =& $_SESSION["pageDefObj"];
    			$obj = $this->pageDefObj;
    			$obj->printSomething();
    		}
    		else
    		{
    			print "<br>session for obj pageDefObj is empty";
    			# create new obj of type pageControl, then assign to session obj
    			print "<br>Setting pageDefObj now<br>";
    			
    			#$this->pageDefObj = new pageControl();
    			
    			#$_SESSION["pageDefObj"] =& $this->$pageDefObj;
    			print "<br>added obj to session";
    		}
    		
    		$_SESSION['pageDefObj'] =& $this->$pageDefObj;
    		
    	}
    	
    }*/
    
    class pageInitialisation{
        var $pageDefObj;
        function pageInitialisation(){
            print "<br><b>New instantiation of pageInitialisation</b>";
            print "<br><br>session has? - " . $_SESSION["pageDefObj"];
    
            $this->pageDefObj = ( isset( $_SESSION['pageDefObj'] ) ) ? $_SESSION['pageDefObj'] : new pageControl() ;
    
            $obj=$this->pageDefObj;
    
            if( method_exists( $obj ,'printSomething' ) ){
                $obj->printSomething();
            }else{
                echo '<pre>';
                print_r( get_class_methods( $obj ) );
                var_export( $obj ) ;
                echo '</pre>';
            }
    
            $_SESSION['pageDefObj'] =& $this->pageDefObj;
        }
    }
    
    
    # baseclass to test ability to grab the session objects
    #$pageInitObj = new pageInitialisation();
    pageInitialisation::pageInitialisation();
    ?>


  •  

    Posting Permissions

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