Ich habe ein Script geschreiben, welches zur Veranschaulichung von OOP dienen soll, ich säubere hier in dem Fall das Haus, sodas $haus->sauberkeit eigentlich 70 sein sollte, es wird jedoch 50 ausgegeben! Was habe ich falsch gemacht?
class.inc.php:
index.php
class.inc.php:
PHP:
<?php
class Haus {
public $name = '';
public $residents = array();
public $rooms = 0;
public $sauberkeit = 50;
public function saeubern($prozent) {
$this->sauberkeit = $this->sauberkeit + $prozent;
if ($this->sauberkeit >= 101) {
$this->sauberkeit = 100;
}
}
function __construct() {
}
}
class Mensch extends Haus{
public $age = 0;
public $name = '';
public $gender = '';
public $home = '';
public $id = 0;
function __construct($name) {
$this->name = $name;
$query = mysql_query("SELECT * FROM menschen WHERE name='".$this->name."'");
if (mysql_num_rows($query) == 1) {
while ($row = mysql_fetch_assoc($query)) {
$this->age = $row['age'];
$this->gender = $row['gender'];
$this->home = $row['home'];
$this->id = $row['id'];
$_SESSION['sessid'] = $row['id'];
}
$query1 = ("SELECT * FROM sessions WHERE id='".$md5ip."'");
if (@mysql_num_rows($query1) == 1) {
$query2 = mysql_query("UPDATE sessions SET menschen='".$this->name."', home='".$this->home."'");
if ($query2 == 0) {
die("Unbekannter Fehler, Code: 0002");
}
} else if (@mysql_num_rows($query1) == 0) {
$query2 = mysql_query("INSERT INTO sessions(menschen, home, id) VALUES('".$this->name."', '".$this->home."', '".$md5ip."')");
if ($query2 = 0) {
die("Unbekannter Fehler, Code: 0003");
}
} else {
die("Unbekannter Fehler, Code: 0004");
}
} else if (mysql_num_rows($query) == 0) {
die("Mensch existiert nicht!");
} else if (mysql_num_rows($query) >= 2) {
die("Mensch existiert mehr als 1 mal");
} else {
die("Unbekannter Fehler, Code: 0001");
}
}
public function hausSaeubern($gegenstand) {
if ($gegenstand == 'Besen'){
$seaubernplus = 20;
parent::saeubern($seaubernplus);
}
}
}
?>
index.php
PHP:
<?php session_start();
$md5ip = md5($_SERVER['REMOTE_ADDR']);
include 'db.inc.php';
include 'conf.inc.php';
include 'class.inc.php';
?>
<html name="html">
<head>
<link rel="stylesheet" href="screen.css" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="script.js"></script>
</head>
<body name="body">
<?php
$mensch = new Mensch('malte');
$haus = new Haus();
$mensch->hausSaeubern('Besen');
echo $haus->sauberkeit;
?>
</body>
</html>