gish Posted October 12, 2008 Share Posted October 12, 2008 hi I have setup this class once it runs, I want it to make a choice and then turn the value as well as add a script. Is this possible? This is what I have so far but I can't get it to work. gish class users_choice_class{ private $userschoice; private $choosing_name; private $construct_name; public function __construct($construct_name) { $this->userschoice = $construct_name; } public function choosing_function($choosing_name) { if ($choosing_name == apple){ include("typesofapples.php") $this->userschoice = $choosing_name; }else{ } } public function choosing_fuction_return() { return $this->userschoice; } } Quote Link to comment Share on other sites More sharing options...
genericnumber1 Posted October 12, 2008 Share Posted October 12, 2008 yes, you can include() files in a method. That should work to include typesofapples.php if the passed $choosing_name is equal to the value in the apple constant. define('apple', 1); $class = new users_choice_class($whatever_construct_name_is); $class->choosing_function(apple); // includes typesofapples.php and sets userschoice to the const val of apple echo $class->userschoice; // outputs 1 I won't comment on the actual structure or naming convention of your class Quote Link to comment Share on other sites More sharing options...
gish Posted October 12, 2008 Author Share Posted October 12, 2008 comment away I am learning and i don't want to get into any bad habits. This is my second attempted so comment away. Thank you for your direction so far. Quote Link to comment Share on other sites More sharing options...
genericnumber1 Posted October 12, 2008 Share Posted October 12, 2008 Well I just can't really see what the point of the class is :X plus the naming convention is off, at least for one variable. userschoice and choosing_name have two different conventions. Quote Link to comment Share on other sites More sharing options...
gish Posted October 12, 2008 Author Share Posted October 12, 2008 conventions is not a term that I can say that I have seen in my reading what do you mean? Yes I am very green to oop! The class is going create multiple choices the come from a session variable that is set by a form. I my other script it has 20 options. I don't know if is a good idea for a class but right now I am redoing my website so I thought I would do all in oop. Thankyou for your direction. It is exactly what I am looking for. Quote Link to comment Share on other sites More sharing options...
genericnumber1 Posted October 12, 2008 Share Posted October 12, 2008 Well I mean naming convention in the sense that you should be consistent in how you name your variables, classes, etc, but you were consistent up until the one variable name, so it's not the end of the world, just set off my nit-picky alarms. Quote Link to comment Share on other sites More sharing options...
gish Posted October 12, 2008 Author Share Posted October 12, 2008 just means you understand the theory properly not like my green code. lol Quote Link to comment Share on other sites More sharing options...
genericnumber1 Posted October 12, 2008 Share Posted October 12, 2008 Everyone's gotta start somewhere I'd advise you as to where to find good sources on proper oop principles, but I learned all of mine through a different language so I don't know any sources that are php specific. Maybe someone else here could give you some more reading material to help out with the theoretical bits since you seem to be doing quite well on your own already. Quote Link to comment Share on other sites More sharing options...
gish Posted October 14, 2008 Author Share Posted October 14, 2008 I have been doing some research and yes my oop was in need of some help. In the below post genericnumber1 placed below example when I need some assistance I understand the rest of what he pointed out but the line below has me perplexed can some one explain to me why I need to define?? define('apple', 1); Quote Link to comment Share on other sites More sharing options...
DarkWater Posted October 14, 2008 Share Posted October 14, 2008 Wait, I have an even better question. Why are you defining the variable passed into the class methods as class variables that you don't even use? Quote Link to comment Share on other sites More sharing options...
genericnumber1 Posted October 15, 2008 Share Posted October 15, 2008 The reason you have to define is because you're passing the value by referencing a constant, which is what I assumed you wanted to do.. if you want to pass it as a variable change $class->choosing_function(apple); to $class->choosing_function($apple); or $class->choosing_function('apple'); and you won't have to define apple as a constant... I figured you wanted to pass it as a variable (or literal, as in the second example.. $apple is a variable, 'apple' is called a literal because its name is its value) in the beginning, but I put the definition in there to see if you could catch your own error Quote Link to comment Share on other sites More sharing options...
gish Posted October 15, 2008 Author Share Posted October 15, 2008 genericnumber1 thanks for the reply. If you see where my oop in princple please point to the code I need to change thanks. Ok i have made some changes but I can't get the if statements to work can anyone see why? and am I get closer to oop design? If I echo the session variable and $set_prop_for_choice they will be the same but I can't get it match. <?php class users_choice_class{ private $set_prop_for_choice; private $construct_name; private $users_choice_php; function __construct($construct_name) { $this->users_choice_php = $construct_name; } public function choosing_function() { $set_prop_for_choice = $users_choice_php; if ($this->set_prop_for_choice == 'addition'){ $this->users_choice_php = include("apples.php"); }else if($this->set_prop_for_choice == 'multiplication' ){ $this->users_choice_php = include("bannana.php"); }else if ($this->set_prop_for_choice == 'subtraction'){ $this->users_choice_php = include("watermelon.php"); }else if ($this->set_prop_for_choice == 'division'){ $this->users_choice_php = include("orange.php"); }else{ echo "<br>sorry there is an issue with our server please contact the sites owner." ; echo "<br>after this point $set_prop_for_choice should be " . $set_prop_for_choice; } } public function choosing_function_return(){ return $this->users_choice_php; return $this->$set_prop_for_choice; } } this is what I am using to call the method echo $_SESSION['users_choice']; $script_choice_class = new users_choice_class($_SESSION['users_choice']); $script_choice_class->choosing_function(); echo $script_choice_class->choosing_function_return(); Quote Link to comment Share on other sites More sharing options...
genericnumber1 Posted October 15, 2008 Share Posted October 15, 2008 A couple of things... $set_prop_for_choice = $users_choice_php; $users_choice_php isn't set, and $set_prop_for_choice isn't what you think it is, try $this->set_prop_for_choice = $this->users_choice_php; Also... $this->users_choice_php = include("apples.php"); When you include a file it doesn't return anything but whether the file include was ok or not... so I have no idea what you're trying to do there. echo "<br>after this point $set_prop_for_choice should be " . $set_prop_for_choice; Again you probably meant echo '<br>after this point $set_prop_for_choice should be ' . $this->set_prop_for_choice; And.. public function choosing_function_return(){ return $this->users_choice_php; return $this->$set_prop_for_choice; } You can't return two values, after you return execution stops... besides, $this->$set_prop_for_choice isn't correct. Quote Link to comment Share on other sites More sharing options...
gish Posted October 15, 2008 Author Share Posted October 15, 2008 thanks again Quote Link to comment Share on other sites More sharing options...
gish Posted October 15, 2008 Author Share Posted October 15, 2008 It works great thanks again. public function choosing_function_return(){ return $this->users_choice_php; return $this->$set_prop_for_choice; } I don't actual need this return $this->$set_prop_for_choice;, but if I wanted to return both properties how would I do that? Quote Link to comment Share on other sites More sharing options...
genericnumber1 Posted October 15, 2008 Share Posted October 15, 2008 Return it as an array perhaps? // etc... public function choosing_function_return(){ return array($this->users_choice_php, $this->set_prop_for_choice); } // etc... // then call it with something like list($value1, $value2) = $object->choosing_function_return(); 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.