Jump to content

OOP Problem!


Kryllster

Recommended Posts

I am trying to understand PHP OOP and I have some code that is not working. here is the error code.

Parse error: parse error, expecting `T_FUNCTION' in C:\wamp\www\testing\armor_lib.php on line 11

 

And here is the armor_lib.php file:

<?php
class armor {
// Head
public $head;
public $torso;
public $pants;
public $gloves;
public $boots;

// new stuff here
class head extends armor {
function __construct($head){
	$this->set_head($head);
}
}
// Torso
class torso extends armor {
function __construct($torso){
	$this->set_torso($torso);
}
}
// Pants
    class pants extends armor {
function __construct($pants){
	$this->set_pants($pants);
}
}
// Gloves
class gloves extends armor {
function __construct($gloves){
	$this->set_gloves($gloves);
}
}
// Boots
class boots extends armor {
function __construct($boots){
	$this->set_boots($boots);
}
}
}
?>

 

And here is the php in the armor.php file I have:

<?php
$head = new armor("Leather Helm");
$torso = new armor("Leather Shirt");
$pants = new armor("Leather Pants");
$gloves = new armor("Chain Mail Gloves");
$boots = new armor("Leather Boots");

echo "You are wearing: " . $head->get_head() . "on your Head";
echo "<br />";
echo "You are wearing: " . $torso->get_torso() . "on your Torso";
echo "<br />";
echo "You are wearing: " . $pants->get_pants() . "on your Legs";
echo "<br />";
echo "You are wearing: " . $gloves->get_gloves() . "on your Hands";
echo "<br />";
echo "You are wearing: " . $boots->get_boots() . "on your Feet";
?>

 

Any Help in understanding this will be much appreciated. Thanks.

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

it looks like you define subclasses within the parent class definition. they need to be moved out.

 

class armor {
// Head
public $head;
public $torso;
public $pants;
public $gloves;
public $boots;
}

// new stuff here
class head extends armor {
function __construct($head){
	$this->set_head($head);
}
}

// Torso
class torso extends armor {
function __construct($torso){
	$this->set_torso($torso);
}
}

// Pants
class pants extends armor {
function __construct($pants){
	$this->set_pants($pants);
}
}

// Gloves
class gloves extends armor {
function __construct($gloves){
	$this->set_gloves($gloves);
}
}

// Boots
class boots extends armor {
function __construct($boots){
	$this->set_boots($boots);
}
}

Link to comment
https://forums.phpfreaks.com/topic/224572-oop-problem/#findComment-1160067
Share on other sites

Here is some code that works but I need to know how I could use a database to get the armor values from it.

<?php
class armor {
   // Head
   public $head;
   public $torso;
   public $pants;
   public $gloves;
   public $boots;

// new stuff here

function set_head($head) { 
$this->head = $head;  
	}
   function get_head() {
return $this->head;
}
function set_torso($torso) {
$this->torso = $torso;
}
function get_torso() {
return $this->torso;
}
function set_pants($pants) {
$this->pants = $pants;
}
function get_pants() {
return $this->pants;
}
function set_gloves($gloves) {
$this->gloves = $gloves;
}
function get_gloves() {
return $this->gloves;
}
function set_boots($boots) {
$this->boots = $boots;
}
function get_boots() {
return $this->boots;
}
}
?>

 

And this is in armor.php

<?php
$head = new armor();
$torso = new armor();
$pants = new armor();
$gloves = new armor();
$boots = new armor();

$head->set_head("Leather Helm");
$torso->set_torso("Leather Shirt");
$pants->set_pants("Leather Pants");
$gloves->set_gloves("Chainmail Gloves");
$boots->set_boots("Platemail Boots");

echo "You are wearing: " . $head->get_head();
echo "<br />";
echo "You are wearing: " . $torso->get_torso();
echo "<br />";
echo "You are wearing: " . $pants->get_pants();
echo "<br />";
echo "You are wearing: " . $gloves->get_gloves();
echo "<br />";
echo "You are wearing: " . $boots->get_boots();
?>

 

 

Like I said this works but I cant help but think there is a better way to do this with less code.

 

Thanks for any advice.

Link to comment
https://forums.phpfreaks.com/topic/224572-oop-problem/#findComment-1160084
Share on other sites

Consider something along the lines of:

 

<?php
class armor {
   // Head
   public $head;
   public $torso;
   public $pants;
   public $gloves;
   public $boots;

// new stuff here
function __construct(array $items) {
	foreach($items as $k => $v) {			
		$this->{$k} = $v;		
	}
}

function set_head($head) { 
	$this->head = $head;  
}

function get_head() {
	return $this->head;
}

function set_torso($torso) {
	$this->torso = $torso;
}

function get_torso() {
	return $this->torso;
}

function set_pants($pants) {
	$this->pants = $pants;
}

function get_pants() {
	return $this->pants;
}

function set_gloves($gloves) {
	$this->gloves = $gloves;
}

function get_gloves() {
	return $this->gloves;
}

function set_boots($boots) {
	$this->boots = $boots;
}

function get_boots() {
	return $this->boots;
}

}

$armor = new armor( array('head' =>'bucket','torso'=>'20cm chainmail','pants'=>'4cm plate','gloves'=>'gauntlet','boots'=>'leather') );

//for use with database
/*

$sql = "SELECT head,torso,pants,gloves,boots FROM armor WHERE id = '1'";
$result = mysql_query($sql) or trigger_error($sql . '<br />' . mysql_error());
if(mysql_num_rows($result) > 0) {
$row = mysql_fetch_assoc($result);
$armor = new armor($row);
}

*/


echo "You are wearing (head): " . $armor->get_head();
echo "<br />";
echo "You are wearing (torso): " . $armor->get_torso();
echo "<br />";
echo "You are wearing (pants): " . $armor->get_pants();
echo "<br />";
echo "You are wearing (gloves): " . $armor->get_gloves();
echo "<br />";
echo "You are wearing (boots): " . $armor->get_boots();
?>

Link to comment
https://forums.phpfreaks.com/topic/224572-oop-problem/#findComment-1160103
Share on other sites

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.