CheesierAngel Posted July 6, 2006 Share Posted July 6, 2006 Is it possible to create classes in PHP with the function body's declared in a different file (like C++) ?For example :FILE1: Class definitionclass Test { public function TestOutlined();}FILE2: Function defintionsTest::TestOutlined() { echo "This is the body of the TestOutlined() function declared into another file!";}Or are there any "work arounds" for seperating the functionbody's from the classes ? Quote Link to comment https://forums.phpfreaks.com/topic/13828-classes-in-php-like-c/ Share on other sites More sharing options...
GingerRobot Posted July 6, 2006 Share Posted July 6, 2006 Well if you include or require the first file in your second, then it would run as if it were one file:include("file1.php"); Quote Link to comment https://forums.phpfreaks.com/topic/13828-classes-in-php-like-c/#findComment-53759 Share on other sites More sharing options...
CheesierAngel Posted July 6, 2006 Author Share Posted July 6, 2006 That seems indeed to be obvious, but i can't find the correct syntax to define the functionbody's outside my classes.I also didn't found any suggestions herefor in the phpmanual. Has someone already used this kind of programming in php before ?If you did, plz can you let me know how to declare the functionbody's outside my classes ? Quote Link to comment https://forums.phpfreaks.com/topic/13828-classes-in-php-like-c/#findComment-53765 Share on other sites More sharing options...
Chips Posted July 6, 2006 Share Posted July 6, 2006 Create an object, then call it's functions/methods. You still need the include statement.[code]$example = new classname();$example->functioncall();[/code]This will call the function "functioncall()" from the object created for example.If you mean something more like this (described as it is for java) where you can have a class with a [i]static[/i] method, which can then be called without constructing an object of that class by the follows...classname.staticmethodsignature();Otherwise you have to create an object to call the methods. Not sure if php can do this, haven't looked - but it would be an idea to lookup static + php in google and see if you can find anything about it. Would still require an include i'd imagine, just like the "import" in java ;) Quote Link to comment https://forums.phpfreaks.com/topic/13828-classes-in-php-like-c/#findComment-53770 Share on other sites More sharing options...
CheesierAngel Posted July 6, 2006 Author Share Posted July 6, 2006 Thanks, i'll 'google' for it. But it seems more and more inpossible to declare the functions like this in PHP.(If i find something intresting i'll post it) Quote Link to comment https://forums.phpfreaks.com/topic/13828-classes-in-php-like-c/#findComment-53774 Share on other sites More sharing options...
Chips Posted July 6, 2006 Share Posted July 6, 2006 It appears that you can do it:[code]<?phpclass Foo { public static function aStaticMethod() { // ... }}Foo::aStaticMethod();?> [/code]So just adding the static keyword to your function declaration should sort your problem out. I say should, but bear in mind I come here to ask for help - so I am not the best person to be handing it out to be honest :P ;) Quote Link to comment https://forums.phpfreaks.com/topic/13828-classes-in-php-like-c/#findComment-53779 Share on other sites More sharing options...
CheesierAngel Posted July 6, 2006 Author Share Posted July 6, 2006 That seems indeed to be working, but it's not really what i want to do.class Foo { public function anOutlinedFunction();}Foo::anOutlinedFunction() { echo "The outlined function !"; return;}$class = new Foo();$class->anOutlinedFunction();Is it even possible to do something like this in PHP ? Quote Link to comment https://forums.phpfreaks.com/topic/13828-classes-in-php-like-c/#findComment-53791 Share on other sites More sharing options...
Chips Posted July 6, 2006 Share Posted July 6, 2006 I was confused tbh, I now get at what you mean - i misunderstood what you were after. Sounds more like the implementation of abstract methods or interfaces, and its the implementing class that requires to define the method body.Fraid my limited brain power and knowledge just can't help, sorry. Probs been more of a diversion in my posts than helpful :( Quote Link to comment https://forums.phpfreaks.com/topic/13828-classes-in-php-like-c/#findComment-53798 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.