Toon posts:

[php] Parent class kan niet in een ander bestand?

Pagina: 1
Acties:

Verwijderd

Topicstarter
Twee dagen geleden stuitte ik op een heel raar probleem. Ik heb een parent class, en daarvan wil ik een child maken. Als ik dat doe, terwijl de class declaration van de parent in hetzelfde bestand staat, werkt het prima.

Include ik dat bestand echter, dan krijg ik:
Fatal error: Cannot instantiate non-existent class: test in d:\webserver\phpgg-2\testmodule\test_module.php on line 4
Nog gekker vind ik dat als ik print_r (get_declared_classes()); uitvoer in het bestand, ik
Array ( [0] => stdClass [1] => __PHP_Incomplete_Class [2] => OverloadedTestClass [3] => Directory [4] => COM [5] => VARIANT [6] => action )
krijg. Terwijl de enige class zou action moeten zijn, dat is de parent, omdat ik de rest in het bestand heb weggecomment. Iemand die me kan helpen want ik ben nogal confused :S

  • thomaske
  • Registratie: Juni 2000
  • Laatst online: 26-06 02:03

thomaske

» » » » » »

weet je zeker dat de include goed werkt?

Brusselmans: "Continuïteit bestaat niet, tenzij in zinloze vorm. Iets wat continu is, is obsessief, dus ziekelijk, dus oninteressant, dus zinloos."


  • D2k
  • Registratie: Januari 2001
  • Laatst online: 09-01 11:25

D2k

Cannot instantiate non-existent class

kortom hij vind dat die class niet bestaat :?

Doet iets met Cloud (MS/IBM)


  • stekkel
  • Registratie: Augustus 2001
  • Laatst online: 08-07 15:10
zoals thomaske al zei, je include werkt niet.
Tip:
* zet error reporting op E_ALL
* gebruik require i.p.v. include

Verwijderd

Topicstarter
- De include werkt wel gewoon. Als ik een echo ("test"); in de file zet wordt dat inderdaad getoond.
- error_reporting (E_ALL); had ik al geprobeerd en geeft geen extra informatie.
- Require had ik ook al aan gedacht, maar ook dat helpt niets.

Ik begrijp er werkelijk niks meer van :?

edit:
even wat duidelijker opschrijven

  • stekkel
  • Registratie: Augustus 2001
  • Laatst online: 08-07 15:10
post eens wat code zou ik zeggen

  • ACM
  • Registratie: Januari 2000
  • Niet online

ACM

Software Architect

Werkt hier

Ik weet uit ervaring dat het wel kan in verschillende files :)
Ik denk dat je ergens een foutje hebt?
test het es zo:
PHP:
1
2
3
4
5
6
7
8
9
10
11
12
13
test.php:
<?
class test
{
}
?>
test2.php:
<?
include('test.php');
class test2 extends test
{
}
?>

Verwijderd

Topicstarter
ACM, wat je zegt had ik inderdaad ook al geprobeerd. Dat werkt dus ook niet. Maar mijn bestand met de child-klasse include nog meer bestanden met klasse-declaraties erin. Die doen het allemaal perfect.

Deze werkt dus alleen niet als ik hem include(require). Ik heb de code er maar bijgevoegd, ik weet niet in hoeverre het te begrijpen is, het zit allemaal redelijk complex in elkaar.

PHP:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
<?php

/*

phpGG Action Class, www.phpgg.nl
  by Peter van der Reijden 

Class Action is a parent class to all parts of a Module.

*/

class Action {
    // Some required objects
    var $tpEngine;
    var $db;
    
    // Action information
    var $key;
    var $description;
    
    // Vars of actions template
    var $templateVars;
    var $templateBlockName;
    var $templateData;

    // C O N S T R U C T O R
    function Action ($key, $description, &$db)
    {
        
        // Instantiate the template engine
        $this->InitTemplate($key);
        $this->SetBlockName ("_ROOT");
        
        // Database object
        $this->db = $db;
        
        // Set Module information
        $this->key = $key;
        //$this->description = $description;
        
    }
    
    // Checks if the user is allowed to access the specified action (aid = action id)
    function UserAllowed (&$user)
    {
    
        if (!$user->UserAllowed ($this->name) )
        {
        
            $this->Error ( ERROR__USER_NOT_ALLOWED );
            return false;
        }
    
        else
        {
            return true;
        }
    }
    
    // Handles error messages
    function Error ( $error )
    {
        // Use standard error template
        $this->tpEngine = new Template ("../templates/action-error.html");
        
        // Set the template Error var to the error which belongs at the error_id (from error_messages.php)
        $this->SetTemplateVar ("errorMessage", $error );
    
    }
    
    // To build the template
    function Build ()
    {
        $this->tpEngine->ParseWithVars($this->templateBlockName, $this->templateVars);
        $this->tpEngine->Parse("_ROOT");
        $this->templateData = $this->tpEngine->GetParsedData ("_ROOT");

        return $this->templateData;
    }
    
    // SetTemplateVar sets a template variable to a value
    function SetTemplateVar ($varName, $varValue)
    {
        $this->templateVars[$varName] = $varValue;
    }
    
    // Set blockname (ie. for edit page block edit, remove page remove etc
    function SetBlockName ($blockName)
    {
        $this->templateBlockName = $blockName;
    }
    
    // Function Execute
    function Execute ()
    {
    
        // has to be overwritten by children
    
    }
    
    function InitTemplate ($name)
    {
        $this->tpEngine = new Template ("../templates/action-". $name . ".html");
    }
}

?>

  • thomaske
  • Registratie: Juni 2000
  • Laatst online: 26-06 02:03

thomaske

» » » » » »

en laat de file eens zien met de include (test_module.php ?)

Brusselmans: "Continuïteit bestaat niet, tenzij in zinloze vorm. Iets wat continu is, is obsessief, dus ziekelijk, dus oninteressant, dus zinloos."


Verwijderd

Topicstarter
PHP:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
<?php

include ("../INCLUDE.php");

$t = new Test ( "test_module" , "description" , &$p->db );

$p = new Page();

$t->Execute();

$p->SetContentReplacement ( $t->Build() );

$p->Build ();

class Test extends Action {
    function Test ($db)
    {
        $this->Action ("test_module", "Test Module", $db);
    }

    function Execute()
    {
        $this->SetBlockName ("Keuze");

        if ($_GET["keuze"])
        {
        
            $this->SetBlockName ("Gekozen");
            
            if ($_GET["keuze"] == "keuze1")
            {
                $this->SetTemplateVar ("analyse", "Kijk! Met jou kan ik praten. Waarom moeilijk doen als het makkelijk kan? Al die mensen die denken dat ze wiskunde kunnen..");
            }
            
            else 
            {
                $this->SetTemplateVar ("analyse", "Man man man... Teveel wiskunde gehad op je middelbare school?? Niet moeilijk doen en simpel houden wat simpel is.. ");

            }
        
        }
        
    }
}
?>


Het ligt er niet aan dat ik het via een algemeen include bestand doe. Dat heb ik ook al anders geprobeerd en dat maakte niks uit.

[ Voor 0% gewijzigd door Verwijderd op 03-10-2002 16:42 . Reden: [ php ] ipv [ code ] ]


  • thomaske
  • Registratie: Juni 2000
  • Laatst online: 26-06 02:03

thomaske

» » » » » »

en je weet echt 100% zeker dat deze bestaat:
PHP:
1
include ("../INCLUDE.php");


:?

[edit]
waarschijnlijk staan in INCLUDE.php wederom includes, omdat deze includes ws ook in ../ staan zal het niet werken. Een mogelijkheid is dus om in je INCLUDE.php voor je includes ../ te zetten

Brusselmans: "Continuïteit bestaat niet, tenzij in zinloze vorm. Iets wat continu is, is obsessief, dus ziekelijk, dus oninteressant, dus zinloos."


  • stekkel
  • Registratie: Augustus 2001
  • Laatst online: 08-07 15:10
in initTemplate staat zo'n verwijzing naar de class Template

Verwijderd

Topicstarter
thomaske schreef op 03 oktober 2002 @ 16:44:
en je weet echt 100% zeker dat deze bestaat:
PHP:
1
include ("../INCLUDE.php");


:?

[edit]
waarschijnlijk staan in INCLUDE.php wederom includes, omdat deze includes ws ook in ../ staan zal het niet werken. Een mogelijkheid is dus om in je INCLUDE.php voor je includes ../ te zetten
Had ik ook al aan gedacht. Begrijp je al waarom ik hier zo gek wordt? :)

PHP:
1
2
3
4
5
6
7
8
9
10
11
12
13
<?php

include ("../lib/class.template.php");
include ("../lib/class.user.php");
include ("../lib/class.action.php");

include ("../CONF.php");
include ("../PAGE.php");
include ("../lib/DB.php");

include ("../LANG_" . CONF_LANGUAGE . ".php");

?>

Verwijderd

Topicstarter
stekkel schreef op 03 oktober 2002 @ 17:06:
in initTemplate staat zo'n verwijzing naar de class Template
Dus :?

  • ACM
  • Registratie: Januari 2000
  • Niet online

ACM

Software Architect

Werkt hier

Misschien helpt het als je eerst de class definieert en dan pas iets van instantieert, zou goed moeten gaan maar je weet maar nooit :)

Edit yups, dan gaat het mis...

PHP:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class.php:
<?
include('class2.php');
$bla = new bla();

class bla extends class2
{
}
?>
class2.php:
<?
class class2
{
}
?>

Gaat fout
en
PHP:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class.php:
<?
include('class2.php');

class bla extends class2
{
}
$bla = new bla();
?>
class2.php:
<?
class class2
{
}
?>

Gaat goed...
Als die class bla geen subclass van class2 is gaat het wel goed.
Lijkt me dus een beetje een bug in php, magoed :)

Verwijderd

Topicstarter
ACM je bent echt mijn held! Hoewel ik het nogal vaag vindt dat het dan wel werkt. Als ik de parent class gewoon boven in het bestand stop en verder niks aan de volgorde verander ( dus de subclass wordt dan al wel geinstantieerd voordat hij is gedefinieerd ) doet hij het wel :?.

Hoe dan ook, bedankt!
Pagina: 1