Jump to content


Photo

Classes in PHP like C++


  • Please log in to reply
7 replies to this topic

#1 CheesierAngel

CheesierAngel
  • Members
  • PipPipPip
  • Advanced Member
  • 105 posts
  • LocationBelgium

Posted 06 July 2006 - 09:06 AM

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 ?


#2 GingerRobot

GingerRobot
  • Staff Alumni
  • Advanced Member
  • 4,086 posts
  • LocationUK

Posted 06 July 2006 - 09:30 AM

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");

#3 CheesierAngel

CheesierAngel
  • Members
  • PipPipPip
  • Advanced Member
  • 105 posts
  • LocationBelgium

Posted 06 July 2006 - 09:39 AM

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 ?

#4 Chips

Chips
  • Members
  • PipPipPip
  • Advanced Member
  • 68 posts

Posted 06 July 2006 - 09:43 AM

Create an object, then call it's functions/methods. You still need the include statement.

$example = new classname();
$example->functioncall();

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 static 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 ;)

#5 CheesierAngel

CheesierAngel
  • Members
  • PipPipPip
  • Advanced Member
  • 105 posts
  • LocationBelgium

Posted 06 July 2006 - 09:54 AM

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)

#6 Chips

Chips
  • Members
  • PipPipPip
  • Advanced Member
  • 68 posts

Posted 06 July 2006 - 10:09 AM

It appears that you can do it:
<?php
class Foo {
   public static function aStaticMethod() {
       // ...
   }
}

Foo::aStaticMethod();
?> 

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 ;)

#7 CheesierAngel

CheesierAngel
  • Members
  • PipPipPip
  • Advanced Member
  • 105 posts
  • LocationBelgium

Posted 06 July 2006 - 10:55 AM

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 ?


#8 Chips

Chips
  • Members
  • PipPipPip
  • Advanced Member
  • 68 posts

Posted 06 July 2006 - 11:28 AM

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 :(





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users