OOP autoload
Lerne OOP und habe mit __autoload ein Problem.
Kann Jemand erkennen, was hier fehlerhaft ist? Danke vor ab!
Mit require_once funktionierst . Die Classen Name sind gleich mit Datei-Name +. php
Ich habe eine Parent-class „Artikel“ (siehe unter)
Child-class „Buch“ (siehe unter)
Child-class „DVD“
Ein Index.Datei gespeichert in index.php
Alle Dateien sind im Ordner class abgelegt!
Meine index.datei beginnt mit:
<?php
function __autoload($class)
{
require 'class/'. $class. '.php'; // dies ist Zeile 17
}
$movie = new DVD('5 vor 12', '2 hr 10 min');
$book = new Buch('Buch ',' Object-Oriented Solution for beginners',300);
echo '<p> Das ' . $book->getProductType() . '"'. $book->getTitle() .
'" hat '. $book->getPageCount(). ' Seiten </p>';
.
.
.
------------------------------------------------------------------ Ende index.datei -------------
Fehler-Nachricht:
Warning: require(class/DVD.php) [function.require]: failed to open stream: No such file or directory in C:\xampp\htdocs\class\index.php on line 17
Fatal error: require() [function.require]: Failed opening required 'class/DVD.php' (include_path='.;C:\xampp\php\PEAR') in C:\xampp\htdocs\class\index.php on line 17
-------------------------------------------------------------------------------------------------
index.php ohne _autoload aber mit require_once:
require_once('DVD.php'); // hiermit ist es OK
require_once('Buch.php'); // " "
/* function __autoload($class)
{
require 'class/'. $class. '.php';
}
*/
$book = new Buch('Buch ',' Object-Oriented Solution for beginners',300);
$movie = new DVD('5 vor 12', '2 hr 10 min');
echo '<p> Das ' . $book->getProductType() . '"'. $book->getTitle() .
'" hat '. $book->getPageCount(). ' Seiten </p>';
echo ……..
.
Ergebnis ist richtig:
Das Buch " Object-Oriented Solution for beginners" hat 300 Seiten
Die DVD " 5 vor 12 " die Spielzeit ist: 2 hr 10 min
------------------------------------Parent!---------------------------------- Artikel.php ------
<?php
class Artikel
{
protected $_type;
protected $_title;
public function __construct($type, $title)
{
$this->_type = $type;
$this->_title = $title;
}
public function getProductType()
{
return $this->_type;
}
public function getTitle()
{
return $this->_title;
}
}
----------------------------------Child!---------------------------------------- Buch.php -----
<?php
require_once('Artikel.php');
class Buch extends Artikel
{
protected $_pageCount;
public function __construct($type, $title, $pageCount)
{
parent::__construct($type, $title);
$this->_pageCount = $pageCount;
}
public function getPageCount()
{
return $this->_pageCount;
}
}
----------------------------------------Child!---------------------------------------- DVD.php -----
<?php
require_once('Artikel.php');
class DVD extends Artikel
{
protected $_duration;
public function __construct($title, $duration)
{
parent::__construct($type, $title);
$this->_type = 'DVD';
$this->_title = $title;
$this->_duration = $duration;
}
public function getDuration()
{
return $this->_duration;
}
}
|