PHP Forum

PHP Forum (http://www.selfphp.de/forum/index.php)
-   PHP für Fortgeschrittene und Experten (http://www.selfphp.de/forum/forumdisplay.php?f=13)
-   -   OOP autoload (http://www.selfphp.de/forum/showthread.php?t=24258)

huberg 28.06.2011 10:52:49

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;
}
}

Ckaos 28.06.2011 11:44:19

AW: OOP autoload
 
Hi

hast du den Code kopiert? ansonsten versteh ich nicht das du ihn nicht lesen kannst.
PHP-Code:

require_once('DVD.php'); // hiermit ist es OK WEIL kein PFAD -> 'class/'
require_once('Buch.php'); // " "

/* function __autoload($class)
{
require 'class/'. $class. '.php';//hiermit nicht ok WEIL PFAD -> 'class/'
}
*/ 

Wenn deine Index nicht im Ordner sondern neben ihm liegt sollte das funktionieren.

mfg

CKaos

huberg 28.06.2011 20:55:51

AW: OOP autoload, erledigt
 
OK Ckaos
Du hast Recht. Index.php nebendran ist die Loesung Danke
Es funktioniert jetzt auch mit autoload


Alle Zeitangaben in WEZ +2. Es ist jetzt 12:07:14 Uhr.

Powered by vBulletin® Version 3.8.3 (Deutsch)
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.