when trying to include a file of small utility methods inside a class definition I get parse error that seems to say you cannot use include('file.xxx'); between
function or method definitions - can't use it outside the scope of a function.

Is it Working As Designed that you may not include('...) complete function definitions in a class definition? You can include the body of a function definition between the 'public function xx {' and the closing '}', so for large functions or methods you can move most of the code to a separate file - but if you have several short functions I don't see how to include them from a single file.

Suggestions? Am I missing something simple?

...Ian.

    It's working as designed: why would you need to "move most of the code to a separate file"? It wouldn't save you anything to do so, and it would make subclassing a lot trickier (which is probably a better way of doing what you want to do anyway).

      well, if not using classes it is easy to push messy detail into functions and display the actual logic as sequence of function calls. By messy details I mean the not very readable mixtures of HTML tags and PHP variables. Overhead of many function calls is balanced by the much improved clarity of the application logic.

      Before using classes I could banish the many little functions into a separate file and include it - so the primary file was the application logic pure and simple. Now, with classes I cannot do that sort of separation of file content, although I could of course make another class and instantiate it (or use a static class and define something short to avoid writing long classname::xxx() names).

      Perhaps subclassing would be an appropriate way to do it - define the utility routines in the superclass and subclass to add the application logic - seems a little strained to just achieve what I want.

      Thanks. ...Ian.

        (The fact that class definitions cannot be separated into multiple files has been present since PHP4).

        Well, I don't really know what you're doing that makes inclusion simpler than existing OO architectures; but you might want to look at the Classkit extension.

          Write a Reply...