I'll try to explain why I feel such a feature as interfaces would be useful
to me in PHP.

This is all under the assumption that when a class is loaded into memory a
certain number of "checks" are performed to make sure it is a valid
definition. For example, You cannot extend a class which is not defined in
memory. Then we could then equally say that you cannot implement an
interface without first defining its functions. We are not violating the
precident of inappropriate access to private members not flagging an error
because this is not an object level error, but instead a class definition
error.

Now let's say that I am creating a standardized class library in which the
root class definition, Object, GUARANTEES a universal means of saving
objects to a database.  Obviously this is an easy problem to solve.
Serialize the object and save it to the database.

But now we add a new feature to our library.  Let's create a new class
called SearchableObject.  We want this to be a type of language construct in
which if I extend this SearchableObject a new table is created in the
database in which all of the variables of the class are mirrored instead of
serialized offering greater search flexibility.

It's funny that you mention private variables in the Zend2 Engine because
that's exactly where my problem arises.  If my class, let's call it Person,
in which I'm extending SearchableObject has private variables (such as name,
address, password).  Obviously we can't make these variables public because
then anyone could change their value.  How do I automagically save them to
the database from within the base class definition?  This is an interesting
problem.  We can't access these variables directly from a method call say,
get_object_vars($this).

One way I've found to do this is with an accessor function getVars.  Here's
an example of what it might look like:

//$child is a double hash as defined below
function getVars($child=""){
   return parent::getVars($child[$this->getClass()] =
array(var_1=>$this->var_1 ... var_n=>var_n)); //Where var_1..n are the class
variable names
}

This function provides a way of passing private variable values to the base
class without compromise of security or loss of redundant variables (i.e.
having the same variable name in the parent and child).  Now if you extend
SearchableObject we can detect it within our database save routine, but
there is still no way for us to know that you have implemented the function
getVars, therefore not guarantying that all objects can be saved
automatically.

If SearchableObject were instead an interface we could put the stub for
getVars within it, and then guarantee that any class which implemented
SearchableObject had the function getVars.  That would bring us one step
closer to our goal.

I'm sorry for the long explanation but I couldn't find a better way to
explain how interfaces could be useful without going a little in depth.  If
you have any suggestions how one would go about requiring a function be
defined in a class without using interfaces please let me know.

Thanks,

Ben Dischinger

----- Original Message -----
From: "Brad LaFountain" <[EMAIL PROTECTED]>
To: "Alan Knowles" <[EMAIL PROTECTED]>; "Wez Furlong" <[EMAIL PROTECTED]>
Cc: "Ben Dischinger" <[EMAIL PROTECTED]>;
<[EMAIL PROTECTED]>
Sent: Wednesday, August 21, 2002 1:05 AM
Subject: Re: [PHP-DEV] Interfaces in PHP


>
> --- Alan Knowles <[EMAIL PROTECTED]> wrote:
> >  From my reading of delegation, it's really overloading, worded slightly
> > differently.. - but not that related to interfaces...
> >
> > In the example below, which is a php'ized version of a C# demo, the
> > advantages of using interfaces (as far as I know) are primarly useful
> > for COM or CORBA, where it would be possible to generate all the
> > components for a CORBA/COM/.NET, or even java binding by reading the
> > interfaces.
> >
> > A .NET binding would effectively define all the available interfaces and
> > classes, with strong typing!, and then when called (from C# or .NET)
> > invoke a C call to call_user_function, to run php exectuter....
> > (probably the best way to implement a .NET server in php, - compiling a
> > loosely typed language into .NET bytecodes, from reading about
> > python,perl etcs. experience is not the way to go..)
> >
> > Other than being nice for documentation, in pure PHP, I cant really see
> > them being much use...
> >
> > Interesting subject anyway..
> >
> > Regards
> > Alan
> >
> > called from C#
> >
> > PHP_Celcius c = new PHP_Celcius(12.9);
> > system.out(c.toFarenheight().toString());
> >
> >
> >
> >
> > Interface temperature {
> >      function __construct(float $value);
> >      float function toCelcius();
> >      float function toFarenheight();
> > }
>
>  There really are two different things here..
>
> 1) "Type Hints" meaning you can define the types of variables
>  This has been discussed before it hasn't been turned down or implemented.
>
> 2) Interfaces as they exist in java don't really give you much in a
stripting
> language but if you insist on having something like that you can curently
do it
> with the zend1.
>
> class temperature {
>       function __construct($value) {}
>       function toCelcius() {}
>       function toFarenheight() {}
>  }
>
> just use that class and extend away....
>
>
>
> but this does bring up an intresting thing... To really implement
interfaces
> when/if delegation is implemented would be easy.
>
> 1) create a interface keyword : which is easy
> 2) in that state allow only function (name)(params)(semi) : i believe this
is
> easy
> 3) internally compile them as normal functions with no implemntation or
see if
> an e_notice can be thrown. : pretty sure this is easy as well
>
> Im not saying that we should but it doesn't seem to be very hard at all. I
> think it exists as neat vs usefull issues.
>
> - Brad
>
> __________________________________________________
> Do You Yahoo!?
> HotJobs - Search Thousands of New Jobs
> http://www.hotjobs.com
>
> --
> PHP Development Mailing List <http://www.php.net/>
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>


-- 
PHP Development Mailing List <http://www.php.net/>
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to