[???] OOP Judging

Status
Not open for further replies.

brsy

nah mang
May 12, 2011
1,530
272
Okay, I am starting OOP, and I am wondering if this would work out.

class_lib.php
PHP:
<?php
class person {
var $name;
function set_name($new_name) {
$this->name = $new_name;
}
function get_name() {
return $this->name;
}
}
?>

index.php
PHP:
<?php include("class_lib.php"); ?>
<?php
$ztriick = new person();
$mace = new person();
$kryptos->set_name("Chris Davis");
$mace->set_name("Mace Schram");
 
echo "Mace's full name: " . $mace->name;
?>
 

Kryptos

prjRev.com
Jul 21, 2010
2,205
1,252
You should not use var when defining class variables. Use Public, Private, etc.
Also, try to indent your code, like so;

PHP:
<?php
class person {
 
      public $name;
 
      function set_name($new_name) {
          $this->name = $new_name;
      }
 
      function get_name() {
          return $this->name;
      }
}
?>

I also noticed an error where you put '$kryptos' instead of '$ztriick', guessing you got confused because you did everything else right.
Also, when using double quotes ( " " ), you can put variables in them, like so;
PHP:
<?php
$ztriick = new person();
$mace = new person();
$ztriick->set_name("Chris Davis");
$mace->set_name("Mace Schram");
 
echo "Mace's full name: {$mace->name}";
echo "zTriick's full name: {$ztriick->name}";
?>

Now, now. You have a get_name function and you aren't using it??????

You could do this!

PHP:
<?php
$ztriick = new person();
$mace = new person();
$ztriick->set_name("Chris Davis");
$mace->set_name("Mace Schram");
 
echo "Mace's full name: {$mace->get_name()}";
echo "zTriick's full name: {$ztriick->get_name()}";
?>

And by doing this, you can set your class variable $name to private! Since now, we don't need to access it outside of the class, but just call it with the get_name() function!

PHP:
private $name;

-----------

Sorry if I confused you, lol. I'm on a roll :cool:
 

brsy

nah mang
May 12, 2011
1,530
272
I think I grasp your point. So I can do this? (Note: I didn't copy and paste the above, I am just trying to go by memory.)
PHP:
<?php
 
$devbest = new person;
 
$devbest->set_link("DevBest.com");
 
echo ("DevBest is a MMORPG forum. If you would like to check them out, here is their link: {Sdevbest->get_link();}");
 
?>
 

Kryptos

prjRev.com
Jul 21, 2010
2,205
1,252
I think I grasp your point. So I can do this? (Note: I didn't copy and paste the above, I am just trying to go by memory.)
PHP:
<?php
 
$devbest = new person;
 
$devbest->set_link("DevBest.com");
 
echo ("DevBest is a MMORPG forum. If you would like to check them out, here is their link: {Sdevbest->get_link();}");
 
?>

Yea, you could do something like that. Just remove the ';' from {$devbest->get_links();} and you're set.
 
Status
Not open for further replies.

Users who are viewing this thread

Top