Jump to content

Recommended Posts

I am trying something simple, creating some horses and making them race using OOP, creating them is easy, how can i find out which one "won" based on the highest total. Here is what i got so far

 

<?php

class Horse
{
   public $strength;
   public $speed;
   public $agility;         

        public function create($name,$age,$odds){
            $this->name=$name;
            $this->odds=$odds;
            $this->strength=(rand(50,200) * $this->odds - $this->age);
            $this->speed=(rand(100,400) * $this->odds - $this->age);
            $this->agility=(rand(150,250) * $this->odds - $this->age);
            $all=array(0=>"$this->strength",1=>"$this->speed",2=>"$this->agility");
            echo "<b>$this->name</b> has a strength of <b>$this->strength</b>, a speed of <b>$this->speed</b> and agility at <b>$this->agility</b>. --- <b>".array_sum($all)."</b><br>";
        }
}

$mix=rand(2,6);
$a=new Horse;
  $a->create("Dave",  2, $mix);
  $a->create("Dude",  3, $mix);
  $a->create("Evan",  3, $mix);
  $a->create("Jill", 15, $mix);
  $a->create("Flex", 10, $mix);

?>

 

I cant figure out a way to find out who had the highest total, i would love to know what i am doing wrong.

Link to comment
https://forums.phpfreaks.com/topic/232267-oop-question/
Share on other sites

Try making 5 different variables, each one a new Horse(), and create() each one.  If you're comfortable with arrays you can put them all in an array, letting you extend it to as many horses as you want.

 

Once you have those 5 object instances you can access the properties of each one.

Link to comment
https://forums.phpfreaks.com/topic/232267-oop-question/#findComment-1194877
Share on other sites

No no no, look,  even though you are both wrong and it creates BRAND new instances of horses, please read my post, how do i find out which horse one, even though u say im making the same horse with different names, it outputs

 

Dave has a strength of 304, a speed of 796 and agility at 450. --- 1550
Dude has a strength of 238, a speed of 590 and agility at 380. --- 1208
Evan has a strength of 218, a speed of 410 and agility at 428. --- 1056
Jill has a strength of 104, a speed of 330 and agility at 302. --- 736
Flex has a strength of 140, a speed of 582 and agility at 414. --- 1136

 

So, seeing as dave won, how can i tell that dave won in my code, how can i rewrite it, if your saying im just calling my horse over and over and i need a whole new class per horse than whats the point of Object oriented programming, since that cant be the case, can someone else help besides those two guys above me

 

Tha guy above me almost answered my question

 

...Once you have those 5 object instances you can access the properties of each one.

 

Ok great how do i access those properties of any of them, a specific one, all of them, what the code like $a->access-->thatvariable->onhorse->2 , thats literally my question

How can i access these instantly created variables

 

if this is too confusing, pretend there is only one horse, after creating him with my class and sending it to the page, how can i access the variable for his strength?

Link to comment
https://forums.phpfreaks.com/topic/232267-oop-question/#findComment-1195022
Share on other sites

BIG SIGH

No, it's the same horse instance. Like thorpe mentioned, you're just changing the horses name and age. If you create different instances for each horse, you can compare the instances with each other.

Who cares if its the same horse instance, ill change it, HOW CAN I COMPARE THEM TOGETHER, how do i find out what horse 2's variables are, and use them like if horse 2 total > horse 1 total

you said

If you create different instances for each horse, you can compare the instances with each other.

Please read my whole post, you literally said EXACTLY what the guy above me said, and didnt answer my reply to him.

 

Should i explain im new to OOP, is getting the variables from a class outside of the class so easy everyone is confused on what i am asking?

 

smaller less confusing for you guys, example

 

class dude{
public $variable;
public $variable2
function usethese(){
$this->$variable=25;
$this->$variable2=100;
}
}

how do i find the new value, not the null value, i tried things like

 

$fuck= new dude;
$fuck->usethese->$this->variable2;
$fuck->usethese-->$variable2;
$fuck->usethese--->variable2;
$fuck--->variable2;
$fuck->variable2;

$differnt= new dude;
$differnt->usethese->$this->variable2;
$differnt->usethese-->$variable2;
$differnt->usethese--->variable2;
$differnt--->variable2;
$differnt->variable2;


if $fuck->usethese->variable2 IS BIGGER THAN, OR SMALLER THAN, OR USING PHP MAX TO FIND HIGHEST VALUE $dude->usethese->variable2 { do some stuff }

 

I do not know how to get the variable outside of the class

Link to comment
https://forums.phpfreaks.com/topic/232267-oop-question/#findComment-1195034
Share on other sites

Who cares if its the same horse instance

If it's the same instance, you *can't* compare them. Which is what you need to do.

 

HOW CAN I COMPARE THEM TOGETHER

There's plenty of ways. Note that after you create a horse you can get their attributes:

$mix=rand(2,6);
$a=new Horse;
$a->create("Dave",  2, $mix);

echo $a->agility;

I would probably create a getPoints() function in the Horse class, which you could then call:

class Horse
{
   //...

   public function getPoints()
   {
        return ($this->strength + $this->speed + $this->agility);
   }
}

 

I would also create a compareTo(Horse $horse) method that would get the total points from the horse in the first parameter and compare it to the current horse.

 

Please read my whole post, you literally said EXACTLY what the guy above me said, and didnt answer my reply to him.

I did read your whole post. You need to follow what they are saying before you get any more help.

Link to comment
https://forums.phpfreaks.com/topic/232267-oop-question/#findComment-1195043
Share on other sites

This is how you access a property of an instance:

 

$mix=rand(2,6);
$a=new Horse;
$a->create("Dave",  2, $mix);
print $a->name;

 

The answer to your original question is still the same though - you can't compare those horses because they are all the same instance.  We answered the question you wrote, not the question you thought you were asking.

 

BTW be careful not to use $this->$variable.  It is $this->variable, without the second $.

Link to comment
https://forums.phpfreaks.com/topic/232267-oop-question/#findComment-1195246
Share on other sites

yay success, thats what i needed, you guys are great, sorry i was frustrated earlier i had just woke up

 

For future users who will indefinitely find this page on some google search years from now as i always find a do, my program above is now solved thanks to the help of you guys, the finished product of my first leap into OOP is below

 

<?php

class Horse
{
   public $strength;
   public $speed;
   public $agility; 
   public $total;         

        public function create($name,$age,$odds){
            $this->name=$name;
            $this->odds=$odds;
            $this->strength=(rand(50,200) * $this->odds - $this->age);
            $this->speed=(rand(100,400) * $this->odds - $this->age);
            $this->agility=(rand(150,250) * $this->odds - $this->age);
            $all=array(0=>"$this->strength",1=>"$this->speed",2=>"$this->agility");
            $this->total=($this->strength + $this->speed + $this->agility);
            echo "<b>$this->name</b> has a strength of <b>$this->strength</b>, a speed of <b>$this->speed</b> and agility at <b>$this->agility</b>. --- <b>".array_sum($all)."</b><br>";
        }
}

$mix=rand(2,6);
$a=new Horse;
  $a->create("Dave",  2, $mix);
$b=new Horse;
  $b->create("Jack",  7, $mix);

if($a->total > $b->total){ print "$a->name Won!"; }else{ print "$b->name Won!"; }
?>

 

Easy cheezy

Link to comment
https://forums.phpfreaks.com/topic/232267-oop-question/#findComment-1195289
Share on other sites

You are not programming OOP, you created a function and named it a class.

 

$dave = Horse::get('Dave'); // new Horse('Dave'); => *MAGIC*
$dude = Horse::get('Dude');
$evan = Horse::get('Evan');
$jill = Horse::get('Jill');
$flex = Horse::get('Flex');

$race = new Race($dave, $dude, $evan, $jill, $flex); // contestants: get ready! set!
$race->start(); // go!
print_r($race->getResults()); // there goes my money!

 

The code for this would look something similar to this:

 

class Horse {
    private function __construct($name, $strength, $speed, $agility) {/*assign variables*/}
    
    public static function get($name) {
        // retrieve Horse from DB, return new instance of self
        return new self($row->name, $row->strength, $row->speed, $row->agility);
    }
}

class Race {
    public function __construct() {
        $this->_addContestants(func_get_args());
        //..
    }
    public function start() {/*let's race!*/}
    public function getResults() {/*race results*/}
}

Link to comment
https://forums.phpfreaks.com/topic/232267-oop-question/#findComment-1195398
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.