Ok, I need a little help. I have these two PHP classes
class menu
{
public static function GetItems ()
{
$res=array();
$r=mysql_query("select * from menuitems");
while($rw=mysql_fetch_row($r))
$res[]=new MenuItem($rw[0],$rw[1],$rw[2],$rw[3]);
return $res;
}
}
And second
class Articles
{
public static function Get ()
{
$res=array();
$r=mysql_query("select * from articles");
while($rw=mysql_fetch_row($r))
$res[]=new Articles($rw[0],$rw[1],$rw[2],$rw[3]);
return $res;
}
}
I need a simple PHP interface class that can write all this inside, just to change MENUITEMS, and articles. Basically I need something like this as an interface
interface Base
{
$res=array();
$r=mysql_query("select * from $base");
while($rw=mysql_fetch_row($r))
$res[]=new $base($rw[0],$rw[1],$rw[2],$rw[3]);
return $res;
}
}
And in the other class I need something that I can calllike this
class menu
{
public static function GetItems ()
{
$baza="Articles";
}
}
You know what I mean, my class Menu needs to implements class baza, and there was $baza, just replace with this string?
Ok, lots of says can not do that in interface, i agree, but i need solution, for my question?
You’re looking for inheritance, rather than “interface.” An interface says that an object that implements it will have certain methods, but must provide the body for them. In your case, you’ll probably do best with an abstract class that has a concrete method of, we’ll say,
Get()and then an abstract method ofGetTable().Then you can
extendthat with your classes. For example:And now you can use
Get()on that object to get the items out of the table.The drawback with doing it this way is that you won’t be able to use a static method — you’ll have to create an instance of your class.