Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old May 10th, 2007, 5:11 PM   #1
kruptof
Professional Programmer
 
kruptof's Avatar
 
Join Date: May 2006
Location: UK - London
Posts: 329
Rep Power: 3 kruptof is on a distinguished road
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:
When I was young it seemed that life was so wonderful,a miracle, oh it was beautiful, magical.
Now watch what you say or they'll be calling you a radical,a liberal, oh fanatical, criminal. Oh won't you sign up your name,we'd like to feel you're acceptable, respectable, oh presentable, a vegetable
kruptof is offline   Reply With Quote
Old May 10th, 2007, 5:50 PM   #2
Styx
Programmer
 
Join Date: Mar 2007
Posts: 39
Rep Power: 0 Styx is on a distinguished road
Are you talking about interfaces?

Read about php object interfaces here
Styx is offline   Reply With Quote
Old May 10th, 2007, 9:55 PM   #3
ReggaetonKing
Sexy Programmer
 
ReggaetonKing's Avatar
 
Join Date: Nov 2005
Location: New Jersey
Posts: 891
Rep Power: 3 ReggaetonKing is on a distinguished road
Send a message via AIM to ReggaetonKing
Yeah, interfaces are the way to go!
__________________
I would love to change the world, but they won't give me the source code!
ReggaetonKing is offline   Reply With Quote
Old May 10th, 2007, 10:38 PM   #4
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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
DaWei is offline   Reply With Quote
Old May 10th, 2007, 10:53 PM   #5
ReggaetonKing
Sexy Programmer
 
ReggaetonKing's Avatar
 
Join Date: Nov 2005
Location: New Jersey
Posts: 891
Rep Power: 3 ReggaetonKing is on a distinguished road
Send a message via AIM to ReggaetonKing
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!
ReggaetonKing is offline   Reply With Quote
Old May 11th, 2007, 11:54 AM   #6
kruptof
Professional Programmer
 
kruptof's Avatar
 
Join Date: May 2006
Location: UK - London
Posts: 329
Rep Power: 3 kruptof is on a distinguished road
Quote:
Originally Posted by reggaeton_king View Post
He's asking for something similar to header files?
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:
Inisde second ( )
Quote:
Originally Posted by DaWei
That isn't at all what the OP is asking.
Is there another way Dawei to do this?
__________________
Quote:
When I was young it seemed that life was so wonderful,a miracle, oh it was beautiful, magical.
Now watch what you say or they'll be calling you a radical,a liberal, oh fanatical, criminal. Oh won't you sign up your name,we'd like to feel you're acceptable, respectable, oh presentable, a vegetable
kruptof is offline   Reply With Quote
Old May 11th, 2007, 12:35 PM   #7
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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
DaWei is offline   Reply With Quote
Old May 11th, 2007, 1:24 PM   #8
kruptof
Professional Programmer
 
kruptof's Avatar
 
Join Date: May 2006
Location: UK - London
Posts: 329
Rep Power: 3 kruptof is on a distinguished road
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:
Fatal error: Access type for interface method iUser::second() must be omitted in C:\wamp\www\interface.php on line xx
[PHP]<?php
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:
When I was young it seemed that life was so wonderful,a miracle, oh it was beautiful, magical.
Now watch what you say or they'll be calling you a radical,a liberal, oh fanatical, criminal. Oh won't you sign up your name,we'd like to feel you're acceptable, respectable, oh presentable, a vegetable
kruptof is offline   Reply With Quote
Old May 11th, 2007, 1:52 PM   #9
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Put the PHP manual on your toolbar.
Quote:
Originally Posted by PHP manual
Object interfaces allow you to create code which specifies which methods a class must implement, without having to define how these methods are handled.

Interfaces are defined using the interface keyword, in the same way as a standard class, but without any of the methods having their contents defined.

All methods declared in an interface must be public, this is the nature of an interface.
__________________
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
DaWei is offline   Reply With Quote
Old May 11th, 2007, 9:44 PM   #10
Styx
Programmer
 
Join Date: Mar 2007
Posts: 39
Rep Power: 0 Styx is on a distinguished road
Oh, was he talking about overriding methods? x_x
Styx is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 6:09 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC