![]() |
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. |
|
Yeah, interfaces are the way to go!
|
That isn't at all what the OP is asking.
|
He's asking for something similar to header files?
|
Quote:
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:
|
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. |
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. |
Put the PHP manual on your toolbar.
Quote:
|
Oh, was he talking about overriding methods? x_x
|
| All times are GMT -5. The time now is 2:59 PM. |
Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC