Jump to content

Archived

This topic is now archived and is closed to further replies.

CheesierAngel

Classes in PHP like C++

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 ?

Share this post


Link to post
Share on other sites
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");

Share this post


Link to post
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 ?

Share this post


Link to post
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 ;)

Share this post


Link to post
Share on other sites
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)

Share this post


Link to post
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 ;)

Share this post


Link to post
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 ?

Share this post


Link to post
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 :(

Share this post


Link to post
Share on other sites

×

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.