Jump to content

include statement inside a class


gish

Recommended Posts

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;
      }

} 

Link to comment
https://forums.phpfreaks.com/topic/128062-include-statement-inside-a-class/
Share on other sites

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 :)

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.

 

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.

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);

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 :D

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();

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.

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?

 

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();

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.