Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   PHP (http://www.programmingforums.org/forum29.html)
-   -   Classes in PHP (http://www.programmingforums.org/showthread.php?t=13139)

kruptof May 10th, 2007 5:11 PM

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.

Styx May 10th, 2007 5:50 PM

Are you talking about interfaces?

Read about php object interfaces here

ReggaetonKing May 10th, 2007 9:55 PM

Yeah, interfaces are the way to go!

DaWei May 10th, 2007 10:38 PM

That isn't at all what the OP is asking.

ReggaetonKing May 10th, 2007 10:53 PM

He's asking for something similar to header files?

kruptof May 11th, 2007 11:54 AM

Quote:

Originally Posted by reggaeton_king (Post 127732)
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?

DaWei May 11th, 2007 12:35 PM

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.

kruptof May 11th, 2007 1:24 PM

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.

DaWei May 11th, 2007 1:52 PM

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.


Styx May 11th, 2007 9:44 PM

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