![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 | |
|
Professional Programmer
Join Date: May 2006
Location: UK - London
Posts: 329
Rep Power: 3
![]() |
Classes in PHP
Can anybody tell how to separate the class specification from the class definition in PHP?
For example in c++ you write the name of the class along with it's data members and prototypes of the class's methods and then you define the methods in a different file and just includes the class specification in that file. how do you do this in PHP, i can write the class with all the methods and data members inside the class but it just gets too long and looks really messy, is there a way to separate them.
__________________
Quote:
|
|
|
|
|
|
|
#2 |
|
Programmer
Join Date: Mar 2007
Posts: 39
Rep Power: 0
![]() |
|
|
|
|
|
|
#3 |
|
Sexy Programmer
|
Yeah, interfaces are the way to go!
__________________
I would love to change the world, but they won't give me the source code! |
|
|
|
|
|
#4 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
That isn't at all what the OP is asking.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#5 |
|
Sexy Programmer
|
He's asking for something similar to header files?
__________________
I would love to change the world, but they won't give me the source code! |
|
|
|
|
|
#6 | |||
|
Professional Programmer
Join Date: May 2006
Location: UK - London
Posts: 329
Rep Power: 3
![]() |
yeah, that's right
Okay thanks for the link, i think it sort of works the way i wanted it to because now i can seperate the specification from the definition, here is what i have at the moment and it works (it gives the correct output): [PHP]<?php interface iUser { public function first(); public function second(); } ?>[/PHP] and then include this ("interface.php") into the below("User.php") [PHP]<?php include("interface.php"); class cUser implements iUser { private $name; public function first() { echo "Inisde First ( )"; } public function second() { echo "Inisde second ( )"; } } ?>[/PHP] and then test it with this file("driver.php") [PHP]<?php include("User.php"); $obj=new cUser; $obj->second(); ?>[/PHP] output: Quote:
Quote:
__________________
Quote:
|
|||
|
|
|
|
|
#7 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
That may be what you wanted, but it isn't what you asked. Note that your definitions of first and second are still inside the class block, not just their declarations or prototypes. All you have done with the interface is force them to be defined.
As far as I know (they seem to change drastically with each release), PHP does not allow you to define functions outside the class block. You can achieve the same effect, somewhat, by defining classes which extend another class.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#8 | ||
|
Professional Programmer
Join Date: May 2006
Location: UK - London
Posts: 329
Rep Power: 3
![]() |
Sorry i think the confusion was due to the the way i worded my first post, but why is it that when i make one of my member functions in the interface private, i get an error saying
Quote:
interface iUser { public function first(); private function second(); } ?>[/PHP] and then include this ("interface.php") into the below("User.php") [PHP]<?php include("interface.php"); class cUser implements iUser { private $name; public function first() { echo "Inisde First ( )"; } private function second() { echo "Inisde second ( )"; } } ?>[/PHP] I have switched to using the extends now, because it doesn't give me that error, but i would like to know why the error had occured when i was using the interface keyword.
__________________
Quote:
|
||
|
|
|
|
|
#9 | |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Put the PHP manual on your toolbar.
Quote:
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
|
#10 |
|
Programmer
Join Date: Mar 2007
Posts: 39
Rep Power: 0
![]() |
Oh, was he talking about overriding methods? x_x
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Could some please explain classes to me... | TCStyle | C++ | 10 | Feb 20th, 2006 3:51 PM |
| What is Lightweight C++? difference in inner classes between C++ and Java? | jonyzz | C++ | 4 | Sep 2nd, 2005 9:56 AM |
| List of classes | Dark_Shinobi | C++ | 8 | May 23rd, 2005 3:50 PM |
| Classes | Sane | Python | 8 | Apr 29th, 2005 12:50 PM |
| C++ Classes - Quick question(s) | Delete | C++ | 8 | Apr 7th, 2005 7:38 PM |