israel love php
israel love php

Reputation: 457

how to create dynamic object variable

did there is some solution instead to write the switch code block for 20 different case. to write code like type of object which will be dynamic variable. the name of the object

Will adjust itself according to the value selected for the forum as value = "option1" , value = "option2" so I will write something like this.

$object = $_POST['option_name'];

And according that I can Create new object witch suitable to his class. And without knowing what his name I can simply call to his method to see what he calculated $object->calculated () //according it is match class And then the code also will suitable to his table name database according the next code. $object->create_record_for_database (); //because each class has a separate table name inside. I hope I'm clear enough Thanks

example code:

<?php 
require_once("/includes/database.php");
require_once("/includes/some_class.php");
?>

<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<br>value1<input type=text name='value1'>
<br>value2<input type=text name='value2'>
<h2>please select one of the choice</h2>
<input type="radio" name="option_name" value="option1" /> "option1"
<br><input type="radio" name="option_name" value=""option2" /> option2
<br><input type="radio" name="option_name" value="option3" /> option3
*
*
*
<br><input type="radio" name="option_name" value="option20" /> option20
<br><input type="submit" name="submit" value="Submit Form"><br>
</form>

<br/><br/>

<?php
$object=""; 
echo("<br>");
        if (isset($_POST['submit'])) {
                if (!empty($_POST['option_name'])) {
                    ClassOption::$option_name = $_POST['option_name'];//can be from option1,option2...option20
                    ClassOption::$value1 = $_POST['value1'];
                    ClassOption::$value2 = $_POST['value2'];

       $option = $_POST['option_name'];
        switch ($option)
{
case option1:
  $option1 = new Option1 ( );// create object1 and calculate it 
  echo 'object1 calculated', $option1->calculated(), "<br />";
  $option1->create_record_for_database();//with value1,value2,option_name
  break;
case option2:
  $option2 = new Option2 ();// create object2 and calculate it
   echo 'object2 calculated', $option2->calculated(), "<br />";
   $option20->create_record_for_database();//with value1,value2,option_name
  break;
  *
  *
  *
  case option20:
   $option2 = new Option2 ();
    echo 'object2 calculated', $option2->calculated(), "<br />";
    $option20->create_record_for_database();//with value1,value2,option_name
}


       }
?>

Upvotes: 0

Views: 491

Answers (1)

TaZ
TaZ

Reputation: 743

If you want to create a new object from a class of which the name is stored in a string, you can use:

$object = new $className();

Or in your case:

$option = new $_POST['option_name']();
$option->calculated();
$option->create_record_for_database();

However, I personally don't like the idea of using variables like this.

Upvotes: 1

Related Questions