mt_rand() not working?

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 1446 Views - Last Post: 16 October 2012 - 08:30 AM Rate Topic: -----

#1 kiasta  Icon User is offline

  • D.I.C Regular

Reputation: 20
  • View blog
  • Posts: 257
  • Joined: 18-November 07

mt_rand() not working?

Posted 13 October 2012 - 08:05 PM

So I'm trying to setup a character class, but for some unknown reason mt_rand() is not working. Is there any reason y'all can see why it would not be working?

user.class.php

<?php
//RNG constants
define(MIN_STAT, 10);
define(MIN_STAT, 20);

//Character creation constants
define(STARTING_LEVEL, 1);
define(STARTING_XP, 0);
define(STARTING_MAX_XP, 200);

class User
{
    private $charLevel,
            $charName,
            $charRace,
            $charClass,
            $curXP,
            $maxXP,
            $curHP,
            $maxHP,
            $curMana,
            $maxMana,
            $curStam,
            $maxStam,
            $strength,
            $intelligence,
            $dexterity,
            $endurance,
            $wisdom;

    public function __construct($charName, $charRace, $charClass)
    {
        $this->create_char($charName, $charRace, $charClass);
    }
    public function set($value, $property)
    {
        $this->set_value($value, $property);
    }
    public function get($property)
    {
        return $this->get_value($property);
    }
    public function stats()
    {
        $stats = $this->get_stats();
        return $stats;
    }
    private function create_char($charName, $charRace, $charClass)
    {
        $this->charLevel = STARTING_LEVEL;
        $this->charName = $charName;
        $this->charRace = $charRace;
        $this->charClass = $charClass;
        $this->curXP = STARTING_XP;
        $this->maxXP = STARTING_MAX_XP;
        $this->strength = mt_rand(MIN_STAT, MAX_STAT);
        $this->intelligence = mt_rand(MIN_STAT, MAX_STAT);
        $this->dexterity = mt_rand(MIN_STAT, MAX_STAT);
        $this->endurance = mt_rand(MIN_STAT, MAX_STAT);
        $this->wisdom = mt_rand(MIN_STAT, MAX_STAT);
        echo $this->strength;
        echo $this->intelligence;
        echo $this->dexterity;
        echo $this->endurance;
        echo $this->wisdom;
        $this->maxHP = (($this->endurance * $this->strength)*(10^2)/2);
        $this->curHP = $this->maxHP;
        $this->maxMana = (($this->intelligence * $this->wisdom)*(6^2)/2);
        $this->curMana = $this->maxMana;
        $this->maxStam = (($this->endurance * $this->dexterity)*(4^2)/2);
        $this->curStam = $this->maxStam;
    }
    private function set_value($value, $property)
    {
        switch($property)
        {
            case 'charLevel': $this->charLevel = $value; break;
            case 'charRace': $this->charRace = $value; break;
            case 'charClass': $this->charClass = $value; break;
            case 'curXP': $this->curXP = $value; break;
            case 'maxXP': $this->maxXP = $value; break;
            case 'curHP': $this->curHP = $value; break;
            case 'maxHP': $this->maxHP = $value; break;
            case 'curMana': $this->curMana = $value; break;
            case 'maxMana': $this->maxMana = $value; break;
            case 'curStam': $this->curStam = $value; break;
            case 'maxStam': $this->maxStam = $value; break;
            case 'strength': $this->strength = $value; break;
            case 'intelligence': $this->intelligence = $value; break;
            case 'dexterity': $this->dexterity = $value; break;
            case 'endurance': $this->endurance = $value; break;
            case 'wisdom': $this->wisdom = $value; break;
            case 'level_up': $this->charLevel += $value; break;
        }
        $this->register();
    }
    private function get_value($property)
    {
        switch($property)
        {
            case 'charLevel': return isset($this->charLevel) ? $this->charLevel: null; break;
            case 'charRace': return isset($this->charRace) ? $this->charRace: null; break;
            case 'charClass': return isset($this->charClass) ? $this->charClass: null; break;
            case 'curXP': return isset($this->curXP) ? $this->curXP: null; break;
            case 'maxXP': return isset($this->maxXP) ? $this->maxXP: null; break;
            case 'curHP': return isset($this->curHP) ? $this->curHP: null; break;
            case 'maxHP': return isset($this->maxHP) ? $this->maxHP: null; break;
            case 'curMana': return isset($this->curMana) ? $this->curMana: null; break;
            case 'maxMana': return isset($this->maxMana) ? $this->maxMana: null; break;
            case 'curStam': return isset($this->curStam) ? $this->curStam: null; break;
            case 'maxStam': return isset($this->maxStam) ? $this->maxStam: null; break;
            case 'strength': return isset($this->strength) ? $this->strength : null; break;
            case 'intelligence': return isset($this->intelligence) ? $this->intelligence : null; break;
            case 'dexterity': return isset($this->dexterity) ? $this->dexterity : null; break;
            case 'endurance': return isset($this->endurance) ? $this->endurance : null; break;
            case 'wisdom': return isset($this->wisdom) ? $this->wisdom : null; break;
        }
    }
    private function calculate_xp($curXP, $maxXP, $charLevel)
    {
        $multiplier = STARTING_MAX_XP;
        for ( ;$curXP > $maxXP; )
        {
            set_value(1, 'level_up');
            $charLevel = get_value('charLevel');
            $maxXP = (($multiplier*$charLevel) + (2^$charLevel));
            set_value($maxXP, 'maxXP');
        }
    }
    private function get_stats()
    {
        echo '<br> Level: ',$this->charLevel;
        echo '<br> Name: ',$this->charName;
        echo '<br> Race: ',$this->charRace;
        echo '<br> Class: ',$this->charClass;
        echo '<br> HP: ',$this->maxHP;
        echo '/',$this->curHP;
        echo '<br> Mana: ',$this->maxMana;
        echo '/',$this->curMana;
        echo '<br> Stamina: ',$this->maxStam;
        echo '/',$this->curStam;
        echo '<br> XP: ',$this->curXP;
        echo '/',$this->maxXP;
        echo '<br> Strength: ',$this->strength;
        echo '<br> Intelligence: ',$this->intelligence;
        echo '<br> Dexterity: ',$this->dexterity;
        echo '<br> Endurance: ',$this->endurance;
        echo '<br> Wisdom: ',$this->wisdom;
    }
    private function register_info()
    {
        //DB Info goes here
    }
}

?>



create_character.php

<?php

require 'user.class.php';
$user = new User($_POST['charName'], $_POST['charRace'], $_POST['charClass']);
$user->stats();

?>


index.php

<html>
    <body>
        <form name="character_creation" action="create_character.php" method="post">
            Character Name: <input type="text" name="charName" />
            Race: <input type="text" name="charRace" />
            Class: <input type="text" name="charClass" />
            <input type="submit" value="Create Character" />
        </form>
    </body>
</html>

<?php
    //insert code here
?>



Everything else seems to be working BUT the mt_rand function. I really don't see why it would not be working. Any suggestions?

Is This A Good Question/Topic? 0
  • +

Replies To: mt_rand() not working?

#2 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2990
  • Posts: 10,332
  • Joined: 08-August 08

Re: mt_rand() not working?

Posted 13 October 2012 - 08:11 PM

What makes you think it's "not working"?
Was This Post Helpful? 0
  • +
  • -

#3 kiasta  Icon User is offline

  • D.I.C Regular

Reputation: 20
  • View blog
  • Posts: 257
  • Joined: 18-November 07

Re: mt_rand() not working?

Posted 13 October 2012 - 08:21 PM

Nevermind I just noticed what the problems was...

//RNG constants
define(MIN_STAT, 10);
define(MIN_STAT, 20);



I can't believe I overlooked something that small...

View PostCTphpnwb, on 13 October 2012 - 11:11 PM, said:

What makes you think it's "not working"?


Sorry I know "not working" is a worthless term. I meant that the mt_rand() function was not running properly. I added values manually without the rand function and it set the values just fine. I found the problem... something really small and stupid that shouldn't have even been a problem.
Was This Post Helpful? 0
  • +
  • -

#4 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2990
  • Posts: 10,332
  • Joined: 08-August 08

Re: mt_rand() not working?

Posted 13 October 2012 - 08:22 PM

Ok, but your get_value function is repetitive.
<?php
class test {
	private $one;
	public $two;
	
	function set_one($value) {
		$this->one = $value;
	}
		
	function get_value($prop) {
		if(isset($this->$prop)) {
			return $this->$prop;
		} else {
			return false;	
		}
	}
}

$obj = new test();
$obj->set_one(10);

$property = array("one", "two");
foreach($property as $a_prop) {
	if($result = $obj->get_value($a_prop)) {
		echo "Property ".$a_prop." is ".$result."<br>";
	} else {
		echo "Result is false, so property ".$a_prop." cannot be returned.<br>";
	}
}

So is set_value, but I'll let you fix that.
Was This Post Helpful? 2
  • +
  • -

#5 kiasta  Icon User is offline

  • D.I.C Regular

Reputation: 20
  • View blog
  • Posts: 257
  • Joined: 18-November 07

Re: mt_rand() not working?

Posted 13 October 2012 - 08:44 PM

Is it not more secure to handle data through private functions instead of public? I suppose since the code is client-side it doesn't really even matter.
Was This Post Helpful? 0
  • +
  • -

#6 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2990
  • Posts: 10,332
  • Joined: 08-August 08

Re: mt_rand() not working?

Posted 13 October 2012 - 08:49 PM

I think you missed the point. Yes, you can make it private, but the important thing is to keep it short and to the point. Also note that it is server side, not client.
Was This Post Helpful? 0
  • +
  • -

#7 kiasta  Icon User is offline

  • D.I.C Regular

Reputation: 20
  • View blog
  • Posts: 257
  • Joined: 18-November 07

Re: mt_rand() not working?

Posted 13 October 2012 - 08:58 PM

View PostCTphpnwb, on 13 October 2012 - 11:49 PM, said:

I think you missed the point. Yes, you can make it private, but the important thing is to keep it short and to the point. Also note that it is server side, not client.


Yeah I meant server-side not client-side. I guess I did miss the point, thanks for clearing that up for me and the advice.
Was This Post Helpful? 0
  • +
  • -

#8 kiasta  Icon User is offline

  • D.I.C Regular

Reputation: 20
  • View blog
  • Posts: 257
  • Joined: 18-November 07

Re: mt_rand() not working?

Posted 14 October 2012 - 08:42 AM

Ok I'm having a little trouble, I took your advice and started from scratch:

user.class.php

<?php
/***********************RNG constants***********************/

define(MIN_STAT, 10);
define(MAX_STAT, 20);

/***********************Character creation constants***********************/

define(STARTING_LEVEL, 1);
define(STARTING_XP, 0);
define(STARTING_MAX_XP, 200);
define(STARTING_STAM, 10);

class User
{    
    private $data = array(
            'charLevel',
            'charName',
            'charRace',
            'charClass',
            'cur_XP',
            'max_XP',
            'cur_HP',
            'max_HP',
            'cur_Mana',
            'max_Mana',
            'cur_Stam',
            'max_Stam',
            'strength',
            'intelligence',
            'dexterity',
            'endurance',
            'wisdom');
    
    public function __construct()
    {
        $this->data['charLevel'] = STARTING_LEVEL;
        $this->data['curXP'] = STARTING_XP;
        $this->data['maxXP'] = STARTING_MAX_XP;
        $this->data['strength'] = mt_rand(MIN_STAT, MAX_STAT);
        $this->data['intelligence'] = mt_rand(MIN_STAT, MAX_STAT);
        $this->data['dexterity'] = mt_rand(MIN_STAT, MAX_STAT);
        $this->data['endurance'] = mt_rand(MIN_STAT, MAX_STAT);
        $this->data['wisdom'] = mt_rand(MIN_STAT, MAX_STAT);
        $this->data['maxHP'] = (($this->data['endurance'] * $this->data['strength'])^2/2);
        $this->data['curHP'] = $this->data['maxHP'];
        $this->data['maxMana'] = (($this->data['intelliegence'] * $this->data['wisdom'])^2/2);
        $this->data['curMana'] = $this->data['maxMana'];
        $this->data['maxStam'] = STARTING_STAM;
        $this->data['curHP'] = $this->data['maxStam'];
    }
    public function stats()
    {        
        foreach($this->data as $property => $value)
        {
            //$value = $this->get_value($property);
            echo "Property ".$property." is ".$value."<br>";
            //echo "".$value."<br>";
        }
    }
    
    public function set_value($property, $value)
    {
        $this->data[$property] = $value;
    }
    public function get_value($property)
    {
        if (isset($this->data[$property]))
        {
            return $this->data[$property];
        }
        else { return false; }
    }
}

?>


create_character.php

<?php

require 'user.class.php';
$user = new User();
foreach ($_POST as $property => $value)
{
    $user->set_value($property, $value);
}
/*foreach($_POST as $property => $key)
{
    $value = $user->get_value($property);
    echo "Property ".$property." is ".$value."<br>";
}*/
$user->stats();
?>



index.php

<html>
    <body>
        <form name="character_creation" action="create_character.php" method="post">
            Character Name: <input type="text" name="charName" />
            Race: <input type="text" name="charRace" />
            Class: <input type="text" name="charClass" />
            <input type="submit" value="Create Character" />
        </form>
    </body>
</html>

<?php
    //insert code here
?>



So I'm thinking that this code is fine until I run the stats() function, I get this output:

Property 0 is charLevel
Property 1 is charName
Property 2 is charRace
Property 3 is charClass
Property 4 is cur_XP
Property 5 is max_XP
Property 6 is cur_HP
Property 7 is max_HP
Property 8 is cur_Mana
Property 9 is max_Mana
Property 10 is cur_Stam
Property 11 is max_Stam
Property 12 is strength
Property 13 is intelligence
Property 14 is dexterity
Property 15 is endurance
Property 16 is wisdom
Property charLevel is 1
Property curXP is 0
Property maxXP is 200
Property strength is 10
Property intelligence is 12
Property dexterity is 10
Property endurance is 11
Property wisdom is 20
Property maxHP is 111
Property curHP is 10
Property maxMana is 1
Property curMana is 1
Property maxStam is 10
Property charName is kiasta
Property charRace is orc
Property charClass is knight


For now I'm not too worried that my algorithm is bugging. I did a little more bug testing, I added a count() function to the stats() function to count the elements in the $data array:

public function stats()
    {
        $arrayCount = count($this->data);
        echo $arrayCount;
        /*foreach($this->data as $property => $value)
        {
            //$value = $this->get_value($property);
            echo "Property ".$property." is ".$value."<br>";
            //echo "".$value."<br>";
        }*/
    }


Now the results are suprising and annoying... I get an output of 33! I don't understand why the output is 33, but it explains why I get the output I was getting before. Am I doing something wrong? Any advise?

*EDIT* I noticed that I left the underscores in the names of the $data array, but I used the underscores as a flag of sorts for strstr(), anyways I removed them and it now looks like this:

private $data = array(
            'charLevel',
            'charName',
            'charRace',
            'charClass',
            'curXP',
            'maxXP',
            'curHP',
            'maxHP',
            'curMana',
            'maxMana',
            'curStam',
            'maxStam',
            'strength',
            'intelligence',
            'dexterity',
            'endurance',
            'wisdom');]


But the $arrayCount is still 33...

**EDIT** Actually upon further debugging I removed the HP and Mana algorithms and just added 15 and 16, respectively, another interesting thing happens:

Property maxHP is 15
Property curHP is 10
Property maxMana is 16
Property curMana is 16


Now if you notice I have the assignemnts like so:

$this->data['maxHP'] = 15/*(($this->data['endurance'] * $this->data['strength'])^2/2)*/;
        $this->data['curHP'] = $this->data['maxHP'];
        $this->data['maxMana'] = 16/*(($this->data['intelliegence'] * $this->data['wisdom'])^2/2)*/;
        $this->data['curMana'] = $this->data['maxMana'];


For some reason I can't determine, curHP equals 10 when it should, according to my code, equal 15. I'm just scratching my head at this point and contemplating taking a day off.

This post has been edited by kiasta: 14 October 2012 - 09:20 AM

Was This Post Helpful? 0
  • +
  • -

#9 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2990
  • Posts: 10,332
  • Joined: 08-August 08

Re: mt_rand() not working?

Posted 14 October 2012 - 09:39 AM

Why are you setting curHP twice, at lines 46 and 50? Since maxStam is set to 10 it makes sense to me that curHP is also 10.
Was This Post Helpful? 0
  • +
  • -

#10 kiasta  Icon User is offline

  • D.I.C Regular

Reputation: 20
  • View blog
  • Posts: 257
  • Joined: 18-November 07

Re: mt_rand() not working?

Posted 14 October 2012 - 10:58 AM

View PostCTphpnwb, on 14 October 2012 - 12:39 PM, said:

Why are you setting curHP twice, at lines 46 and 50? Since maxStam is set to 10 it makes sense to me that curHP is also 10.


Well that would make sense.. I didn't even see that. That was not intentional I just forgot to change the name after copy/pasting.
Was This Post Helpful? 0
  • +
  • -

#11 kiasta  Icon User is offline

  • D.I.C Regular

Reputation: 20
  • View blog
  • Posts: 257
  • Joined: 18-November 07

Re: mt_rand() not working?

Posted 14 October 2012 - 02:39 PM

I have finally fixed all my problems and here is the code for those who are curious:

character.class.php

<?php
/***********************RNG constants***********************/

define(MIN_STAT, 10);
define(MAX_STAT, 20);

/***********************Character creation constants***********************/

define(STARTING_LEVEL, 1);
define(STARTING_XP, 0);
define(STARTING_MAX_XP, 200);
define(STARTING_STAM, 10);

class User
{    
    private $data = array(
            'Level',
            'Name',
            'Race',
            'Class',
            'XP',
            'max_XP',
            'Strength',
            'Intelligence',
            'Dexterity',
            'Endurance',
            'Wisdom',
            'HP',
            'max_HP',
            'Mana',
            'max_Mana',
            'Stamina',
            'max_Stam');
    
    private $arrayElem = array(
            'Level',
            'Name',
            'Race',
            'Class',
            'XP',
            'max_XP',
            'Strength',
            'Intelligence',
            'Dexterity',
            'Endurance',
            'Wisdom',
            'HP',
            'max_HP',
            'Mana',
            'max_Mana',
            'Stamina',
            'max_Stam');
    
    public function __construct()
    {
        $element = $this->arrayElem;
        foreach ($element as $property)
        {
            switch ($property)
            {
                case 'Level': $this->set_value($property, STARTING_LEVEL); break;
                case 'XP': $this->set_value($property, STARTING_XP); break;
                case 'max_XP': $this->set_value($property, STARTING_MAX_XP); break;
                case 'Strength': $this->set_value($property, $this->get_rand()); break;
                case 'Intelligence': $this->set_value($property, $this->get_rand()); break;
                case 'Dexterity': $this->set_value($property, $this->get_rand()); break;
                case 'Endurance': $this->set_value($property, $this->get_rand()); break;
                case 'Wisdom': $this->set_value($property, $this->get_rand()); break;
                case 'max_HP': $this->set_value($property, $this->get_value('HP')); break;
                case 'HP': $this->set_value($property, $this->algorithm($property)); break;
                case 'max_Mana': $this->set_value($property, $this->get_value('Mana')); break;
                case 'Mana': $this->set_value($property, $this->algorithm($property)); break;
                case 'Stamina': $this->set_value($property, STARTING_STAM); break;
                case 'max_Stam': $this->set_value($property, $this->get_value('Stamina')); break;
            }
        }
    }
    private function get_rand()
    {
        return mt_rand(MIN_STAT, MAX_STAT);
    }
    public function stats()
    {
        $element = $this->arrayElem;
        foreach($element as $property)
        {
            $value = $this->get_value($property);
            $searchArray = strstr($property, '_', true);
            if ($searchArray == 'max')
            {
                echo '/'.$value."";
            }
            else if ($searchArray != 'max')
            {
                echo "<br>".$property.": ".$value."";
            }
        }
    }
    private function algorithm($property)
    {
        switch($property)
        {
            case 'HP': { return number_format((($this->get_value('Endurance') * $this->get_value('Strength'))/10*5)); break; }
            case 'Mana': { return number_format((($this->get_value('Intelligence') * $this->get_value('Wisdom'))/10*3)); break; }
            default: {return 0; }
        }
    }
    public function set_value($property, $value)
    {
        $this->data[$property] = $value;
    }
    public function get_value($property)
    {
        if (isset($this->data[$property]))
        {
            return $this->data[$property];
        }
        else { return false; }
    }
}
?>


process_character.php

<?php

require 'user.class.php';
$user = new User();
$userInput = array();
foreach ($_POST as $postElem => $value)
{
    $userInput[] = $postElem;
}
foreach ($userInput as $property)
{
    $user->set_value($property, $_POST[$property]);
}
/*foreach($_POST as $property => $key)
{
    $value = $user->get_value($property);
    echo "Property ".$property." is ".$value."<br>";
}*/
$user->stats();
?>



index.php

<html>
    <body>
        <form name="character_creation" action="create_character.php" method="post">
            Character Name: <input type="text" name="Name" />
            Race: <input type="text" name="Race" />
            Class: <input type="text" name="Class" />
            <input type="submit" value="Create Character" />
        </form>
    </body>
</html>

<?php
    //insert code here
?>



I still have a lot of work to do: failsafes, security loopholes, etc. Thanks a lot for your help once again!
Was This Post Helpful? 0
  • +
  • -

#12 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2990
  • Posts: 10,332
  • Joined: 08-August 08

Re: mt_rand() not working?

Posted 14 October 2012 - 02:45 PM

Couldn't you do this:
foreach ($_POST as $postElem => $value)
{
    $userInput[] = $postElem;
}
foreach ($userInput as $property)
{
    $user->set_value($property, $_POST[$property]);
}

as this?
foreach ($_POST as $property => $value)
{
    $user->set_value($property, $value);

}


Was This Post Helpful? 2
  • +
  • -

#13 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2990
  • Posts: 10,332
  • Joined: 08-August 08

Re: mt_rand() not working?

Posted 14 October 2012 - 02:59 PM

Oh, and set_value() should do some validation. At a minimum you should verify that the property exists before setting a value for it.
Was This Post Helpful? 1
  • +
  • -

#14 kiasta  Icon User is offline

  • D.I.C Regular

Reputation: 20
  • View blog
  • Posts: 257
  • Joined: 18-November 07

Re: mt_rand() not working?

Posted 14 October 2012 - 04:37 PM

View PostCTphpnwb, on 14 October 2012 - 05:45 PM, said:

Couldn't you do this:
foreach ($_POST as $postElem => $value)
{
    $userInput[] = $postElem;
}
foreach ($userInput as $property)
{
    $user->set_value($property, $_POST[$property]);
}

as this?
foreach ($_POST as $property => $value)
{
    $user->set_value($property, $value);

}



I did that because I was getting weird number that turned out to have nothing to do with that part of the code.You're right, that is better, I had it like that originally I just never changed it back.
Was This Post Helpful? 0
  • +
  • -

#15 kiasta  Icon User is offline

  • D.I.C Regular

Reputation: 20
  • View blog
  • Posts: 257
  • Joined: 18-November 07

Re: mt_rand() not working?

Posted 16 October 2012 - 08:22 AM

View PostCTphpnwb, on 14 October 2012 - 05:59 PM, said:

Oh, and set_value() should do some validation. At a minimum you should verify that the property exists before setting a value for it.


So my code looks like this:

<?php
require 'character.class.php';
$character = new Character();
foreach ( $_POST as $property => $value )
{
    if ( property_exists ( get_class ( $character ) , 'data' ) )
    {
        $character -> set_value ( $property , $value );
    }
    else
    {
        echo "<br>Variable does not exist.";
    }
}
/* foreach($_POST as $property => $key)
  {
  $value = $user->get_value($property);
  echo "Property ".$property." is ".$value."<br>";
  } */
$character -> stats ();
?>


Everything is in working order, the question I have is how do I determine the class member: $data without knowing anything about it?

What I mean is something like this:

property_exists ( get_class ( $character ) , $member )


so I don't actually have to know the name of the class member. I was thinking about something like:

get_class_vars ( get_class ( $character ) )


but if I want to add more members than I would have to change the code so it won't bug up. Sure, I could put in a check for the name but that would defeat the whole purpose of not knowing the name of the member in the first place.

Here is a more detailed example of what I am talking about:

$class_vars = get_class_vars( get_class ( $character ) );
$member = '';
foreach ($class_vars as $name => $value)
{
    $member = $name;
}
foreach ( $_POST as $property => $value )
{
    if ( property_exists ( get_class ( $character ) , $member ) )
    {
        $character -> set_value ( $property , $value );
    }
    else
    {
        echo "<br>Variable does not exist.";
    }
}


I've searched through google and other sources but I just can't find exactly what I'm looking for. Any suggestions?

If I have to just use 'data' than I guess that's fine... it's only just 1 extra piece of code I have to change if I want to make changes, but I'd rather just change as little code as possible, if I do need to change some things around.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2