Results 1 to 13 of 13

Thread: php problem - returning a class from a function

  1. #1
    Pseudo-Mad Scientist Whiternoise's Avatar
    Join Date
    Apr 2006
    Location
    Surrey
    Posts
    4,274
    Thanks
    166
    Thanked
    386 times in 233 posts
    • Whiternoise's system
      • Motherboard:
      • DFI LANPARTY JR P45-T2RS
      • CPU:
      • Q6600
      • Memory:
      • 8GB DDR2
      • Storage:
      • 5.6TB Total
      • Graphics card(s):
      • HD4780
      • PSU:
      • 425W Modu82+ Enermax
      • Case:
      • Silverstone TJ08b
      • Operating System:
      • Win7 64
      • Monitor(s):
      • Dell 23" IPS
      • Internet:
      • 1Gbps Fibre Line

    php problem - returning a class from a function

    Currently i've got a working database class for MySql interaction, the function that i use is:

    Code:
    function newdb()
    {
            //host, username variables etc are defined here but i removed for security
    
    	
    	$db = new db();
    	$connection = $db->connect($dbhost, $dbusername, $dbpassword, $db_name);
    	
    	return $db;
    }
    That works fine, i often use it to call things like $db->query($sql); and whatnot.

    What i'm currently trying to do is check whether a user is logged in, what authorisation level they are and then return a user class that i can then do things with (classes: guest, user extends guest, mod extends user, admin extends user - you get the idea)

    The function i'm trying to use to create a new $user dependent on the rank that's stored in the database is:
    Code:
    function createuser()
    {
    // if the user is logged in, create the user based on rank and set all the details (from the database) as class properties
    	if(isset($_SESSION['logged_in']))
    	{
    		if($_SESSION['userrank'] == 1)
    		{
    		$user = new user($_SESSION['userid']);
    		$user->setuserdetails();
    		}
    		elseif($_SESSION['userrank'] == 2)
    		{
    		$user = new moderator($_SESSION['userid']);	
    		$user->setuserdetails();
    		}
    		elseif($_SESSION['userrank'] == 3)
    		{
    		$user = new administrator($_SESSION['userid']);
    		$user->setuserdetails();	
    		}
    	}
    	else
    	{
    		$user = new guest();	
    	}
    	
    	return $user;
    }
    The session variables are fine (i've checked them) and are set upon login. I've done a little debugging and i've found that without logging in, it appears to set the $user variable but it doesn't seem to like returning it properly.

    Running this:

    Code:
    	
    else
    	{
    		$user = new guest();	
    		$user->testecho();
    	}
    Also works as it should (i.e. just before the function returns $user) and echoes out a test message to the browser.

    Simply creating a user in the main page works, and i can call methods fine, it's just when i try to get it from the function it plays up. Am i missing something obvious?

  2. #2
    Gentoo Ricer
    Join Date
    Jan 2005
    Location
    Galway
    Posts
    11,048
    Thanks
    1,016
    Thanked
    944 times in 704 posts
    • aidanjt's system
      • Motherboard:
      • Asus Strix Z370-G
      • CPU:
      • Intel i7-8700K
      • Memory:
      • 2x8GB Corsiar LPX 3000C15
      • Storage:
      • 500GB Samsung 960 EVO
      • Graphics card(s):
      • EVGA GTX 970 SC ACX 2.0
      • PSU:
      • EVGA G3 750W
      • Case:
      • Fractal Design Define C Mini
      • Operating System:
      • Windows 10 Pro
      • Monitor(s):
      • Asus MG279Q
      • Internet:
      • 240mbps Virgin Cable

    Re: php problem - returning a class from a function

    Shouldn't you be doing session initialisation way at the start of the page loading, instead of while you're trying to create a new user?
    Quote Originally Posted by Agent View Post
    ...every time Creative bring out a new card range their advertising makes it sound like they have discovered a way to insert a thousand Chuck Norris super dwarfs in your ears...

  3. #3
    Pseudo-Mad Scientist Whiternoise's Avatar
    Join Date
    Apr 2006
    Location
    Surrey
    Posts
    4,274
    Thanks
    166
    Thanked
    386 times in 233 posts
    • Whiternoise's system
      • Motherboard:
      • DFI LANPARTY JR P45-T2RS
      • CPU:
      • Q6600
      • Memory:
      • 8GB DDR2
      • Storage:
      • 5.6TB Total
      • Graphics card(s):
      • HD4780
      • PSU:
      • 425W Modu82+ Enermax
      • Case:
      • Silverstone TJ08b
      • Operating System:
      • Win7 64
      • Monitor(s):
      • Dell 23" IPS
      • Internet:
      • 1Gbps Fibre Line

    Re: php problem - returning a class from a function

    The session is always started right at the top of the page (i've got an include file that deals with the top part of each page and session_start() is called as the first line), so i'm not sure what you mean. The session variables listed there are all set when the user is logged in - userid is pulled from the database and used to grab everything else like username, authorisation and whatnot.

  4. #4
    Gentoo Ricer
    Join Date
    Jan 2005
    Location
    Galway
    Posts
    11,048
    Thanks
    1,016
    Thanked
    944 times in 704 posts
    • aidanjt's system
      • Motherboard:
      • Asus Strix Z370-G
      • CPU:
      • Intel i7-8700K
      • Memory:
      • 2x8GB Corsiar LPX 3000C15
      • Storage:
      • 500GB Samsung 960 EVO
      • Graphics card(s):
      • EVGA GTX 970 SC ACX 2.0
      • PSU:
      • EVGA G3 750W
      • Case:
      • Fractal Design Define C Mini
      • Operating System:
      • Windows 10 Pro
      • Monitor(s):
      • Asus MG279Q
      • Internet:
      • 240mbps Virgin Cable

    Re: php problem - returning a class from a function

    Ug, that's a butt ugly legacy practice from before PHP had actual class support. Reorganise your user class to call session_start() on object initialisation (the __constructor function).

    I'm also a bit confuse with the way you're doing some things. You seem to be wrapping procedural code around your classes, which makes no sense to me. Why don't you do all that checking *inside* your classes? Make use of constructor functions for your classes if you want to execute code when a new object is created.

    I'm not criticising or anything, just giving you some pointers to keep your code from becoming heavily fragmented. If everything is more cohesive you'll have an easier time of bug hunting.

    Anyway, see: http://www.php.net/manual/en/language.oop5.decon.php
    Quote Originally Posted by Agent View Post
    ...every time Creative bring out a new card range their advertising makes it sound like they have discovered a way to insert a thousand Chuck Norris super dwarfs in your ears...

  5. Received thanks from:

    Whiternoise (27-06-2009)

  6. #5
    Senior Member
    Join Date
    Jan 2009
    Location
    London
    Posts
    350
    Thanks
    6
    Thanked
    48 times in 42 posts
    • sadbuttrue's system
      • Motherboard:
      • Gigabyte P55 UD5
      • CPU:
      • i5 750 @ 3.8ghz + Megahalems + GT1450 @ 5v
      • Memory:
      • G.Skill ECO 4GB 1.4v
      • Storage:
      • Intel 320 120gb + Samsung F4 2tb in Scythe Quiet Drive
      • Graphics card(s):
      • 6950 @ 940/1350/1.175v + Shaman @ 20%
      • PSU:
      • Corsair AX750
      • Case:
      • Raven RV02 with 3xPhobya G-Silent Fans @ 210rpm + Scythe S-Flex 1200 @ 5v
      • Operating System:
      • Windows 7
      • Monitor(s):
      • Samsung 2233rz, Dell 2209wa, Panasonic AX200 Projector
      • Internet:
      • BeThere 24mb

    Re: php problem - returning a class from a function

    There is nothing in the code provided that shouldn't work. The only thing I can see is a missing logic step for logged in users ('else' for when/if userrank isn't set properly).

  7. #6
    Pseudo-Mad Scientist Whiternoise's Avatar
    Join Date
    Apr 2006
    Location
    Surrey
    Posts
    4,274
    Thanks
    166
    Thanked
    386 times in 233 posts
    • Whiternoise's system
      • Motherboard:
      • DFI LANPARTY JR P45-T2RS
      • CPU:
      • Q6600
      • Memory:
      • 8GB DDR2
      • Storage:
      • 5.6TB Total
      • Graphics card(s):
      • HD4780
      • PSU:
      • 425W Modu82+ Enermax
      • Case:
      • Silverstone TJ08b
      • Operating System:
      • Win7 64
      • Monitor(s):
      • Dell 23" IPS
      • Internet:
      • 1Gbps Fibre Line

    Re: php problem - returning a class from a function

    Ok i'll give that a go

    At the moment each page loads:

    1. start session
    2. grab any necessary includes (functions and classes)
    3. create a database object (i've seen some user classes that incorporate the db connection, but i need it for other stuff so it's separate - and passed to the user class as necessary)
    4. create a user object
    5. do all the general content gumph

    OOP confuses the bejesus out of me

    I'm still not quite sure how i should do this though, would i be right in saying..

    Code:
    	function user($sessionuserid,$db)
    	{
    		$this->userid = $sessionuserid;
    		$this->db = $db;
    	
    		$sql = "SELECT * FROM user WHERE userid = '$this->userid'";
    		$result = $this->db->query($sql);
    		$userarray = $this->db->FetchArray($result);
    	
    		foreach ($userarray as $field => $value)
    		{
    			$this->$field = $value;
    		}
    	}
    
    So this would effectively be the construct method..
    The reason i did it the way it was, was i'm sure someone told me it was better practice to have separate methods for setting properties rather than bunging it all in one (i.e. have separate SetVALUE and getVALUE methods for each thing).

    EDIT: Actually ok i remember why this confused me - how would i sort it in the constructor so that it makes a new object dependent on rank? Surely i'd just end up making a constructor that returns a class and i could end up with an infinite loop? All i want is that when the user logs in, i can pull the rank from the database and then on each page i create the user and if the rank is either zero or undefined, create a guest and if it's not then create a user/mod/admin as required.

    And also, if i'm creating an object based on something that's stored in the session, surely i'd have to call session_start before i create the object?

    Or am i overcomplicating things and you were only referring to "setuserdetails" upon construction rather than in the other function?
    Last edited by Whiternoise; 27-06-2009 at 09:17 PM.

  8. #7
    Gentoo Ricer
    Join Date
    Jan 2005
    Location
    Galway
    Posts
    11,048
    Thanks
    1,016
    Thanked
    944 times in 704 posts
    • aidanjt's system
      • Motherboard:
      • Asus Strix Z370-G
      • CPU:
      • Intel i7-8700K
      • Memory:
      • 2x8GB Corsiar LPX 3000C15
      • Storage:
      • 500GB Samsung 960 EVO
      • Graphics card(s):
      • EVGA GTX 970 SC ACX 2.0
      • PSU:
      • EVGA G3 750W
      • Case:
      • Fractal Design Define C Mini
      • Operating System:
      • Windows 10 Pro
      • Monitor(s):
      • Asus MG279Q
      • Internet:
      • 240mbps Virgin Cable

    Re: php problem - returning a class from a function

    No, you store the rank in the class, and the created object will retain it until you delete it or execution finishes.
    Quote Originally Posted by Agent View Post
    ...every time Creative bring out a new card range their advertising makes it sound like they have discovered a way to insert a thousand Chuck Norris super dwarfs in your ears...

  9. #8
    Pseudo-Mad Scientist Whiternoise's Avatar
    Join Date
    Apr 2006
    Location
    Surrey
    Posts
    4,274
    Thanks
    166
    Thanked
    386 times in 233 posts
    • Whiternoise's system
      • Motherboard:
      • DFI LANPARTY JR P45-T2RS
      • CPU:
      • Q6600
      • Memory:
      • 8GB DDR2
      • Storage:
      • 5.6TB Total
      • Graphics card(s):
      • HD4780
      • PSU:
      • 425W Modu82+ Enermax
      • Case:
      • Silverstone TJ08b
      • Operating System:
      • Win7 64
      • Monitor(s):
      • Dell 23" IPS
      • Internet:
      • 1Gbps Fibre Line

    Re: php problem - returning a class from a function

    Sorry to be a pain, but could you be a bit more specific (or possibly give an example of how the code should flow?)?

    Should i just have one big class rather than extending a basic "user" to classes which have methods that need more privileges? If i store the rank in the class - going with an extendable system - i'd still have to construct the object based upon what rank the user is?

    oops realised i edited after you posted, but anyway

    EDIT: Oh and i realised what the stupid mistake was at the start - i simply constructed the object, i forgot to assign it to a variable first - so of course whenever i tried to call a method it moaned that $user was non-object.
    Last edited by Whiternoise; 27-06-2009 at 09:56 PM.

  10. #9
    Gentoo Ricer
    Join Date
    Jan 2005
    Location
    Galway
    Posts
    11,048
    Thanks
    1,016
    Thanked
    944 times in 704 posts
    • aidanjt's system
      • Motherboard:
      • Asus Strix Z370-G
      • CPU:
      • Intel i7-8700K
      • Memory:
      • 2x8GB Corsiar LPX 3000C15
      • Storage:
      • 500GB Samsung 960 EVO
      • Graphics card(s):
      • EVGA GTX 970 SC ACX 2.0
      • PSU:
      • EVGA G3 750W
      • Case:
      • Fractal Design Define C Mini
      • Operating System:
      • Windows 10 Pro
      • Monitor(s):
      • Asus MG279Q
      • Internet:
      • 240mbps Virgin Cable

    Re: php problem - returning a class from a function

    It's no problem.

    Code:
    class user {
        $userid;
        $username;
        $sessionid;
        $rank;
        $db;
    
    __constructor($db,any,other,variables,you,want,to,pass) {
        start_session();
        $this->db = $db;
        // user initalisation block or call an init_user function
        $this->userid = $_SESSION['userid'];
        // save any other variables
        }    
    }
    
    // initialise database connection
    $user = new user($db);
    // do page stuff
    Now if you want to access any user details you can just do if($user->rank > 1) for e.g. to determine whether to load mod/admin code.

    I hope this loose example helps.
    Last edited by aidanjt; 27-06-2009 at 11:42 PM.
    Quote Originally Posted by Agent View Post
    ...every time Creative bring out a new card range their advertising makes it sound like they have discovered a way to insert a thousand Chuck Norris super dwarfs in your ears...

  11. #10
    Pseudo-Mad Scientist Whiternoise's Avatar
    Join Date
    Apr 2006
    Location
    Surrey
    Posts
    4,274
    Thanks
    166
    Thanked
    386 times in 233 posts
    • Whiternoise's system
      • Motherboard:
      • DFI LANPARTY JR P45-T2RS
      • CPU:
      • Q6600
      • Memory:
      • 8GB DDR2
      • Storage:
      • 5.6TB Total
      • Graphics card(s):
      • HD4780
      • PSU:
      • 425W Modu82+ Enermax
      • Case:
      • Silverstone TJ08b
      • Operating System:
      • Win7 64
      • Monitor(s):
      • Dell 23" IPS
      • Internet:
      • 1Gbps Fibre Line

    Re: php problem - returning a class from a function

    Ah ok that makes sense.

    So, are you saying that instead of creating a user from inputs, i should just go ahead and create the object and construct it based on whatever happens to be in the session?

    Out of interest though, is there anything intrinsically wrong about my first approach (having an extensible class) besides the procedural bumph?

  12. #11
    Gentoo Ricer
    Join Date
    Jan 2005
    Location
    Galway
    Posts
    11,048
    Thanks
    1,016
    Thanked
    944 times in 704 posts
    • aidanjt's system
      • Motherboard:
      • Asus Strix Z370-G
      • CPU:
      • Intel i7-8700K
      • Memory:
      • 2x8GB Corsiar LPX 3000C15
      • Storage:
      • 500GB Samsung 960 EVO
      • Graphics card(s):
      • EVGA GTX 970 SC ACX 2.0
      • PSU:
      • EVGA G3 750W
      • Case:
      • Fractal Design Define C Mini
      • Operating System:
      • Windows 10 Pro
      • Monitor(s):
      • Asus MG279Q
      • Internet:
      • 240mbps Virgin Cable

    Re: php problem - returning a class from a function

    Quote Originally Posted by Whiternoise View Post
    So, are you saying that instead of creating a user from inputs, i should just go ahead and create the object and construct it based on whatever happens to be in the session?
    Yup, you can still make the class generic enough to create as many users as you'd like, and manage your session or theirs, or change user details. Just create an object for each user you want to manipulate. Or you could create a child class from the user parent class, and specialise it for current user sessions only, and create an object out of that instead.

    Quote Originally Posted by Whiternoise View Post
    Out of interest though, is there anything intrinsically wrong about my first approach (having an extensible class) besides the procedural bumph?
    It's not that you couldn't do it that way, it's just an approach that's strongly discourage for reasons of maintainability.
    Quote Originally Posted by Agent View Post
    ...every time Creative bring out a new card range their advertising makes it sound like they have discovered a way to insert a thousand Chuck Norris super dwarfs in your ears...

  13. #12
    Pseudo-Mad Scientist Whiternoise's Avatar
    Join Date
    Apr 2006
    Location
    Surrey
    Posts
    4,274
    Thanks
    166
    Thanked
    386 times in 233 posts
    • Whiternoise's system
      • Motherboard:
      • DFI LANPARTY JR P45-T2RS
      • CPU:
      • Q6600
      • Memory:
      • 8GB DDR2
      • Storage:
      • 5.6TB Total
      • Graphics card(s):
      • HD4780
      • PSU:
      • 425W Modu82+ Enermax
      • Case:
      • Silverstone TJ08b
      • Operating System:
      • Win7 64
      • Monitor(s):
      • Dell 23" IPS
      • Internet:
      • 1Gbps Fibre Line

    Re: php problem - returning a class from a function

    Righto, thanks for all this!

  14. #13
    Not a good person scaryjim's Avatar
    Join Date
    Jan 2009
    Location
    Gateshead
    Posts
    15,196
    Thanks
    1,232
    Thanked
    2,290 times in 1,873 posts
    • scaryjim's system
      • Motherboard:
      • Dell Inspiron
      • CPU:
      • Core i5 8250U
      • Memory:
      • 2x 4GB DDR4 2666
      • Storage:
      • 128GB M.2 SSD + 1TB HDD
      • Graphics card(s):
      • Radeon R5 230
      • PSU:
      • Battery/Dell brick
      • Case:
      • Dell Inspiron 5570
      • Operating System:
      • Windows 10
      • Monitor(s):
      • 15" 1080p laptop panel

    Re: php problem - returning a class from a function

    I'm intrigued as to what you use these user-type objects for, tbh - you seem to have all the main properties stored in session variables anyway! You don't need to create the objects to control the flow of the program - you could just check $_SESSION['userrank'] - so presumably these classes / objects do something else? How do they differ from each other?

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. DVI problem, pc won't start! help needed.
    By snowwolf in forum Graphics Cards
    Replies: 1
    Last Post: 09-04-2010, 04:11 PM
  2. XFX 6800GT Problem
    By nvisage in forum SCAN.care@HEXUS
    Replies: 45
    Last Post: 07-08-2006, 12:28 AM
  3. Replies: 23
    Last Post: 08-08-2005, 11:05 AM
  4. php problem...
    By Joel in forum Software
    Replies: 2
    Last Post: 12-10-2003, 11:39 PM
  5. Replies: 4
    Last Post: 19-09-2003, 08:25 AM

Posting Permissions

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