Results 1 to 10 of 10
-
25th Jul 2010, 12:32 PM #1OPMemberWebsite's:
litewarez.net litewarez.com triniwarez.com[PHP] Interfaces
What are they?
Interfaces a block-less classes that are used to make sure other classes contain certain methods and structure, the reason we use these is so that when we have a set of classes in projects we make sure that no matter what the method will exists.
What do they look like?
Here is an example:
PHP Code:interface Sample
{
public function Method($value);
}
How there used
Whan you create a class you use the keyword "implements" to tell the class that its using an interface.
Example:
PHP Code:class MyClass Implements SomeInterface
{
}
Interface:
PHP Code:interface IDatabase
{
public function connect($host,$username,$password);
public function openDatabase($database);
public function query($query);
}
so for the classes now you can have multiple classes the inherit the structure from the interface.
MySql
PHP Code:class DBMySql implements IDatabase
{
private $Connection,$Query;
public function connect($host,$username,$password)
{
$this->Connection = mysql_connect($host,$username,$password,true);
}
public function openDatabase($database)
{
mysql_select_db($database);
}
public function query($query)
{
return $this->Query = mysql_query($query);
}
}
PHP Code:class DBMySqli implements IDatabase
{
private $Connection,$Query;
public function connect($host,$username,$password)
{
$this->Connection = new MySqli($host,$username,$password,true);
}
public function openDatabase($database)
{
$this->Connection->select_db($database);
}
public function query($query)
{
return $this->Query = $this->Connection->query($query);
}
}
So usage lets say
PHP Code://MySql
$DBMySql = new DBMySql ();
//MySqli
$DBMySqli = new DBMySqli();
//No matter witch one i use they would always have the same functions, so $DBMySql->connect does the same thing as $DBMySqli->connect, apart from it uses different inner logic...
//BUT THEY WILL ALLWAYS BE THE SAME TO THE USER.
litewarez Reviewed by litewarez on . [PHP] Interfaces What are they? Interfaces a block-less classes that are used to make sure other classes contain certain methods and structure, the reason we use these is so that when we have a set of classes in projects we make sure that no matter what the method will exists. What do they look like? Here is an example: interface Sample Rating: 5Join Litewarez.net today and become apart of the community.
Unique | Clean | Advanced (All with you in mind)
Downloads | Webmasters
Notifications,Forum,Chat,Community all at Litewarez Webmasters
-
25th Jul 2010, 12:46 PM #2
-
25th Jul 2010, 01:10 PM #3MemberWebsite's:
Doxsters.netI kinda get it, but could you give an example of a function in class MySqli, eg how would you use/modify the functions in IDatabase?
Need a Designer/Web Developer? Click Me
MSN: PM me for it.
Email(Preferred):timtamboy63@gmail.com
"Power Corrupts. Absolute Power Corrupts Absolutely"
-
25th Jul 2010, 01:22 PM #4OPMemberWebsite's:
litewarez.net litewarez.com triniwarez.comUpdate with more code and an extra block.
Join Litewarez.net today and become apart of the community.
Unique | Clean | Advanced (All with you in mind)
Downloads | Webmasters
Notifications,Forum,Chat,Community all at Litewarez Webmasters
-
25th Jul 2010, 01:28 PM #5MemberWebsite's:
Doxsters.netHm I still don't get it, Wouldn't it be the exact same whether you had 'Implements IDatabase' in there or not? After all, you are redifining the functions right?
I guess it might be useful for backwards compatibility, but you don't need the interface for that do you?
Need a Designer/Web Developer? Click Me
MSN: PM me for it.
Email(Preferred):timtamboy63@gmail.com
"Power Corrupts. Absolute Power Corrupts Absolutely"
-
25th Jul 2010, 01:35 PM #6OPMemberWebsite's:
litewarez.net litewarez.com triniwarez.comyou do, take your database class and the method i posted afterwords.
you have a database layer and then sub layers such as mysql, oracle etc
lets say they all implemented a interface that made sure that they worked ok with the database class, and then the user wanted to add another one for lets say xml, as long as they implement the interface it would be fully compatible with the database class.
another example:
PHP Code:interface ElectricalDevice{
public function power_on();
public function power_off();
}
interface FrequencyTuner{
public function get_frequencey();
public function set_frequency($f);
}
class ElectricFan implements ElectricalDevice{
// define ElectricalDevice...
}
class MicrowaveOven implements ElectricalDevice{
// define ElectricalDevice...
}
class StereoReceiver implements ElectricalDevice, FrequencyTuner{
// define ElectricalDevice...
// define FrequencyTuner...
}
class CellPhone implements ElectricalDevice, FrequencyTuner{
// define ElectricalDevice...
// define FrequencyTuner...
}
Join Litewarez.net today and become apart of the community.
Unique | Clean | Advanced (All with you in mind)
Downloads | Webmasters
Notifications,Forum,Chat,Community all at Litewarez Webmasters
-
25th Jul 2010, 01:42 PM #7Member
Well, when you are declaring interfaces you should also specify parameters data type (in case its instance of another class, in case it is just a string or integer it doesnt matter)
PHP ignore it however interfaces are here generally for making extensions, modules and plugins into applications. This makes much easier understand application design.
PHP Code:interface IDatabaseAdapter
{
public function query(IDatabaseQuery $query);
}
Btw, you can also inherit interfaces
PHP Code:interface IAdapter
{
public function register();
}
interface IDatabaseAdapter extends IAdapter
{
public function query(IDatabaseQuery $query);
}
-
25th Jul 2010, 01:53 PM #8OPMemberWebsite's:
litewarez.net litewarez.com triniwarez.comSpecifying the datatype is not really needed unless your passing in classes / object but if i was do do that i would create a data type class for each datatype
PHP Code:class String implements Multilingual
{
public $value;
function __construct($string)
{
$this->value = $string;
}
function __toString($string)
{
return $this->value;
}
function __call($method,$args)
{
if(function_exists($method))
{
$this->value = $method($this->value);
}
return $this;
}
}
$myString = new String("Hello World");
$myString->trim();
PHP Code:class Array implements Iterator
{
//...methods
}
Join Litewarez.net today and become apart of the community.
Unique | Clean | Advanced (All with you in mind)
Downloads | Webmasters
Notifications,Forum,Chat,Community all at Litewarez Webmasters
-
25th Jul 2010, 02:00 PM #9Member
I wouldnt do that. Since php is case-nonsensitive it might cause some troubles, but last time i tried it was ok i think, maybe small issues, but worked. And this solution would be too .NETish
-
25th Jul 2010, 02:05 PM #10OPMemberWebsite's:
litewarez.net litewarez.com triniwarez.comyea but having methods directly available for data types is very nice, but yea PHP is not the best language to do this.
Join Litewarez.net today and become apart of the community.
Unique | Clean | Advanced (All with you in mind)
Downloads | Webmasters
Notifications,Forum,Chat,Community all at Litewarez Webmasters
Sponsored Links
Thread Information
Users Browsing this Thread
There are currently 1 users browsing this thread. (0 members and 1 guests)
themaPoster - post to forums and...
Version 5.36 released. Open older version (or...