aut0tek Posted May 12, 2008 Share Posted May 12, 2008 Okay, I have 2 .php's one includes the other... the First file has variables that need to be utilized in the second... Here's how it goes: CLASS class{ function myfunction() $variables = here include file2.php //end of function // end of class SECOND FILE: //I want to be able to access the vars in class's myfunction. These variables were set immediately before my execution, and I would love to have them aboard! lol //thanks. Quote Link to comment Share on other sites More sharing options...
aut0tek Posted May 12, 2008 Author Share Posted May 12, 2008 I think that maybe my 2nd file has a few includes that cause it to no longer be inside the class/function of the first file... so in other words... how do I get the variables from OUSTIDE of the class and/or function? Quote Link to comment Share on other sites More sharing options...
aut0tek Posted May 12, 2008 Author Share Posted May 12, 2008 :-( Quote Link to comment Share on other sites More sharing options...
BlueSkyIS Posted May 12, 2008 Share Posted May 12, 2008 write a function for the class that returns whatever variables you need. Quote Link to comment Share on other sites More sharing options...
aut0tek Posted May 12, 2008 Author Share Posted May 12, 2008 it returns the variables perfectly for other functions in the class... They can just get them by: function($name, $name2, $name3) //etc etc etc However, outside of that class, they are not seen no matter how hard I try. lol. :-( Quote Link to comment Share on other sites More sharing options...
947740 Posted May 12, 2008 Share Posted May 12, 2008 Make the variables global? Quote Link to comment Share on other sites More sharing options...
aut0tek Posted May 12, 2008 Author Share Posted May 12, 2008 They are! That's what baffled me... 1st file: class myclass{ function myfunction() { $my_email, $my_name, $my_user, $my_pw; $my_name = $row->name; } } 2nd file: $custom_lastname = $my_name; However, when called, the variable is null. :-( Quote Link to comment Share on other sites More sharing options...
BlueSkyIS Posted May 12, 2008 Share Posted May 12, 2008 where do you define them as global? i'd probably make them class vars using $this->$variable_name inside the function, then return the same from inside the function. Quote Link to comment Share on other sites More sharing options...
aut0tek Posted May 12, 2008 Author Share Posted May 12, 2008 sry... at the top of function, I didn't cut/paste, I typed... and I missed the important part... function GLOBAL then the vars. ;-) Hey... this may be something I need to address... I am calling the other php file by it's FULL URL.... not /home/mystuff/etcetcetc Think that may cause it? if I call it by: require_once '/home/mystuff/' it gives me a db error (the script within is accessing a SQL db.) Quote Link to comment Share on other sites More sharing options...
craygo Posted May 12, 2008 Share Posted May 12, 2008 You would need to return the variable then call it. You won't be able to assign variables AND include something in the same function unless you put everything into an array and break it up when assigned. CLASS class{ function my_var_function(){ $variables = here; return $variables; } function my_include(){ // use variables from other function $vars = $this->my_var_function(); include file2.php; } } now you can assign the variables in your scrip <?php $class = new class; // assign your vars $vars = $class->my_var_function(); // now include the page $class ->my_include(); ?> You can't assign a function result to a variable AND have it echo out something. Do one or the other. Ray Quote Link to comment Share on other sites More sharing options...
aut0tek Posted May 12, 2008 Author Share Posted May 12, 2008 Okay, so now that I've got that, how do I access the vars in the 2nd file? like... let's say the 1st file had a variable named $name... in the 2nd file I wanna make a variable with the same string inside it without knowing the string... soo... $class->name ? Quote Link to comment Share on other sites More sharing options...
craygo Posted May 12, 2008 Share Posted May 12, 2008 If your first file is nothing but a class(which it should be) you can get the result from the class on any page you include it in. The variables in the class can be named anything you like when you call a function in the class you assign it to a variable in you main script. so if inside the function my_var_function() you assign a value to $name. You don't call $name you call the function that assigned it. Example: file1.php = class file2.php = page to be displayed so file1.php you leave alone once all your class is complete. You call the class from any other page file2.php <?php include('file1.php'); // This is the class to be included $class = new class; $name = $class-> my_var_function(); ?> Ray Quote Link to comment Share on other sites More sharing options...
aut0tek Posted May 12, 2008 Author Share Posted May 12, 2008 BTW thanks for helping me out, Ray. I been banging my head against a wall with this. I am NOT a php guy, however, I'm getting the hang of the simple stuff. Soooo... let's say there are 5 variables within the my_var_function... First things first, how do u return them? like this: return array($name, $phone, $zip, $address, $etc) ? secondly, in the 2nd file I can just call them like this? $name = $class->my_var_function(); $phone = $class->my_var_function(); etc? that seems too easy. ;-) Quote Link to comment Share on other sites More sharing options...
craygo Posted May 12, 2008 Share Posted May 12, 2008 well almost if you know which one is which you would do this $vars = $class->my_var_function(); // get the array from the class and assign it to $vars $name = $vars[0]; //name is the first value in the array "0" $phone = $vars[1]; // phone is the second value in the array "1" Ray Quote Link to comment Share on other sites More sharing options...
aut0tek Posted May 12, 2008 Author Share Posted May 12, 2008 ooooh I see... tricky... almost done so I can test 'em. Quote Link to comment Share on other sites More sharing options...
aut0tek Posted May 12, 2008 Author Share Posted May 12, 2008 eew, okay btw, I have to include the 2nd file from the 1st, it can't be the other way around... Here's what's going on, in a nutshell... User registers in the CMS, which the CMS does some hash/salt jazz and throws it into a db... I need to grab those same variables it's using, and plant them into my 2nd php script so that it can do some similar, but not equal stuff, and throw it into IT'S sql db... So, basically, From page 1 (all of page 1's stuff works effortlessly, but that's because I didn't write it LOL) and then call page 2, and make it sing the same tune. :-) I think they call this a "bridge" ? Quote Link to comment Share on other sites More sharing options...
craygo Posted May 12, 2008 Share Posted May 12, 2008 Well the easiest thing to do in this case would be to use sessions. First you would have to start your session. **important** You need to put session_start() as the first line in the pages you want to use the sessions so on the first page <?php session_start(); $_SESSION['name'] = $name; $_SESSION['phone'] = $phone; ?> Now as long as you have not skipped any session_starts you can get those variables from the second page <?php session_start() $name = $_SESSION['name']; $phone = $_SESSION['phone']; ?> Now as long as you keep putting session_start() at the top of your pages, those values will be available to you for the users entire stay at your site Ray Quote Link to comment Share on other sites More sharing options...
aut0tek Posted May 12, 2008 Author Share Posted May 12, 2008 In the 2nd file, when I do: $class = new class; the script doesn't end up doing anything... if I take it out, it will run, insert a table into the db, it just has nothing in the variables. :-/ Quote Link to comment Share on other sites More sharing options...
aut0tek Posted May 12, 2008 Author Share Posted May 12, 2008 oooooohhhh.... niiice. How can I tell if it's already IN a session though? Because it probably begins the "logged in" portion immediately before I call my 2nd file. You're awesome, btw. lol. Quote Link to comment Share on other sites More sharing options...
craygo Posted May 12, 2008 Share Posted May 12, 2008 echo the session either print_r($_SESSION); Will return the array, sometimes a little hard to read or loop through it foreach($_SESSION as $key => $value){ echo "SESSION $key has value of $value<br>"; } Ray Quote Link to comment Share on other sites More sharing options...
aut0tek Posted May 12, 2008 Author Share Posted May 12, 2008 So... the 2nd file, even though it's not the beginning of the session, still gets the session_start()? this is new territory. ;-) VB 3.0 was nothing like this ;-) Quote Link to comment Share on other sites More sharing options...
craygo Posted May 12, 2008 Share Posted May 12, 2008 yes session_start() acts like the start, continue whatever as long as you keep putting it at the top of your pages the values will be available throughout your pages whether first or 20th. Ray Quote Link to comment Share on other sites More sharing options...
aut0tek Posted May 12, 2008 Author Share Posted May 12, 2008 hmm... interestingly enough... virtuemart's ps_shopper.php uses session variables but no start up top... I am currently reading on how sessions work. lol. it must be the auto-start deal??? If so, how do I get it to pass over to my php (it is NOT saving the session variables. :-( Quote Link to comment Share on other sites More sharing options...
aut0tek Posted May 12, 2008 Author Share Posted May 12, 2008 Bah. :-( No matter how hard I try, viz the barebones function/class stuff, or the session stuff, I'll never get it. :-( it's been 3 days now, I need to just hire a freelancer I suppose. :-( Thanks anyways, guys. I think I give up on this one. Quote Link to comment Share on other sites More sharing options...
craygo Posted May 12, 2008 Share Posted May 12, 2008 would be glad to help you out. I am leaving the office in about 20 minutes but you can catch me on AIM tomorrow from 8:30am - 4:30pm EST. My screen name is craygo69. I would just need the code you are using for the pages. Ray Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.