|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
[2004-02-22 22:45 UTC] sniper@php.net
|
|||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Fri Nov 28 08:00:01 2025 UTC |
Description: ------------ "I am not sure whether this is a bug, a feature, or lack of a feature." The class must be declared in a global scope. But there no explanation about class declarations that are included in function or in method of other class. If I would like to declare a class inside other class or function, I will have an error. But the strange is that, if I make an include into a function or in class method, then all is working fine: my new class declaration (included from other file) become normal and I can use the class as global declared class. The question is: why class declaration not permitted inside the functions, but it is possibly to load it in the function using "include(...)" command. Examples(1): ------- class_in_method.php ---------- <?php //start class glob { var $i; function glob1() { class incl { function incl_test() { return "result!!"; } } $i=new incl(); echo $i->incl_test(); } }; $g=new glob(); $g->glob1(); //end ?> well, it not works, as listed in all manuals. It shows error: Fatal error : Class declarations may not be nested. ---- end example(1) ----------- But when we are using include("...") Examples(2): -------- class_in_method_included.php ------------- <?php //start class glob { var $i; function glob1() { include("incl_class.inc"); // class incl // { // function incl_test() // { // return "result!!"; // } // } $i=new incl(); echo $i->incl_test(); } }; $g=new glob(); $g->glob1(); ?> //where "incl_class.inc" file contains: <?php class incl { function incl_test() { return "result!!"; } } //end ?> this example works normal and returns as expected: result!! ---- end example(2) ----------- all i'v found in bug lists that similar to my request is Bug #11835, but it is different to my question (man try to include content of class, not the whole class declaration as i'm doing). Expected result: ---------------- result!!