Jump to content

Recommended Posts

Is it possible to create classes in PHP with the function body's declared in a different file (like C++) ?
For example :

FILE1: Class definition
class Test {
  public function TestOutlined();
}

FILE2: Function defintions
Test::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 ?
Link to comment
https://forums.phpfreaks.com/topic/13828-classes-in-php-like-c/
Share on other sites

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 ?
Link to comment
https://forums.phpfreaks.com/topic/13828-classes-in-php-like-c/#findComment-53765
Share on other sites

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 ;)
Link to comment
https://forums.phpfreaks.com/topic/13828-classes-in-php-like-c/#findComment-53770
Share on other sites

It appears that you can do it:
[code]
<?php
class 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 ;)
Link to comment
https://forums.phpfreaks.com/topic/13828-classes-in-php-like-c/#findComment-53779
Share on other sites

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 ?
Link to comment
https://forums.phpfreaks.com/topic/13828-classes-in-php-like-c/#findComment-53791
Share on other sites

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 :(
Link to comment
https://forums.phpfreaks.com/topic/13828-classes-in-php-like-c/#findComment-53798
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.