[PHP] Re: PHP5 and static attributes

2004-05-13 Thread Rudy Metzger
On Thu, 2004-05-13 at 11:41, Gregory wrote:
> Rudy Metzger wrote:
> 
> >Although you are correct with your explanations to better use methods
> >and private/protected attributes, this is not the core of the problem.
> >The problem is that self:: always refers to the current class (A or B or
> >C), so if you would call self::Something() in C it would not access
> >A::Something(). The same counts for parent::. It only accesses the
> >instance DIRECTLY below. 
> >
> >An yes, A::$myInstance is easy to remember. But what if I put it into B
> >at some later points, because I am shifting around class structure (for
> >whatever well thought out reason)? Then I would have to change all
> >occurances of A::$myInstance, which I really do not want.
> >
> >I think a good solution for this would be to modify parent::, that it
> >does not only access its DIRECT superclass, but goes up the hierarchy
> >until the static variable is found.
> >
> >Cheerio
> >/rudy
> >
> >
> I understand what you want to do (or to avoid). Yes, by directly access 
> static
> functions/properties, class hierarchy becomes useless and doesn't allow 
> a complete
> modularity anymore.
> Then the parent:: keyword must go up until a matching function/variable 
> is found.
> You're totally right on this.
> 
> Greg

Actually only modifying parent:: does not solve it, because the static
could be in self:: - thus either self:: also has to go up the hierarchy
or a new keyword (static::, class::) is invented. Or even better: Why
not allow this:: to access static variables, e.g. this::$myInstance.
Would make the most sense, i guess.

Cheerio
/rudy

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] default and another constructor

2004-05-12 Thread Rudy Metzger
On Wed, 2004-05-12 at 15:18, Mark Constable wrote:
> On Wed, 12 May 2004 10:43 pm, Lieve Vissenaeken wrote:
> > Please ,could anybody help me ? I'm not so familiar with PHP.
> >
> > I've the following problem with my code  when I try to make an object from
> > the class "forum" with the code "$test=new forum()".  I always get a
> > warning on this: "Warning: Missing argument 1 for forum() in
> > /lvdata/www/tennis/php/sql.inc"
> > Is it not possible to just make a default constructor and an other
> > constructor like in JAVA ?
> > Thanks for helping
> 
> No, you cannot do this kind of method overloading, not like 
> this anyway. If you are using PHP5 you can kind of emulate 
> overloading by using the __call() function... some googling 
> will find examples.
> 
> You can at least make the below work by removing the first
> forum() instance and using
> 
>  function forum($naam=NULL,$tijd=NULL,$tekst=NULL)
> 
> and test the incoming variables with isset() before attempting
> to use any of them.
If you assign default values to the method arguments, you cannot test
them with isset() anymore, as they will be set.
> 
> > class forum
> > {
> >  var $naam;
> >  var $tijd;
> >  var $tekst;
> >
> >  function forum()
> >  {
> >  }
> >
> >  function forum($naam,$tijd,$tekst)
> >  {
> >   $this->naam=$naam;
> >   $this->tijd=$tijd;
> >   $this->tekst=$tekst;
> > }
> > }
> >
> >
> > $test=new forum();
> 
> --markc


signature.asc
Description: This is a digitally signed message part


Re: [PHP] default and another constructor

2004-05-12 Thread Rudy Metzger
Strange that you get this error. Normally you should get a "Fatal error:
Cannot redeclare forum::forum() "

PHP does not support 'function overloading', at least not in a way java
is doing it. One of the drawbacks of a free typed langauge (in contrary
to a strictly typed one).

cheerio
/rudy

On Wed, 2004-05-12 at 14:43, Lieve Vissenaeken wrote:
> Please ,could anybody help me ? I'm not so familiar with PHP.
> 
> I've the following problem with my code  when I try to make an object from
> the class "forum" with the code "$test=new forum()".  I always get a warning
> on this: "Warning: Missing argument 1 for forum() in
> /lvdata/www/tennis/php/sql.inc"
> Is it not possible to just make a default constructor and an other
> constructor like in JAVA ?
> Thanks for helping
> 
> 
> class forum
> {
>  var $naam;
>  var $tijd;
>  var $tekst;
> 
>  function forum()
>  {
>  }
> 
> 
>  function forum($naam,$tijd,$tekst)
>  {
>   $this->naam=$naam;
>   $this->tijd=$tijd;
>   $this->tekst=$tekst;
> }
> }
> 
> 
> $test=new forum();


signature.asc
Description: This is a digitally signed message part


Re: [PHP] Methods for instatiating an object

2004-05-12 Thread Rudy Metzger
On Wed, 2004-05-12 at 12:41, Jordi Canals wrote:
> Hi all,
> 
> It is not a big issue, but that is something that I never had clear. 
> I've been looking at the manual and found no answer, so I will ask with 
> an example:
> 
> When instantiating an object, I could do it in two different ways:
> 
> A)$object = new MyClass;
> B)$object =& new MyClass;
> 
> I cannot understand the exect difference betwen the two methods. Because 
> there is not yet an object, the second example does not return a 
> reference to an existing object.
> 
> I think perhaps the diference is:
> 
> 1) In case A, PHP creates a new object and returns a Copy of this new 
> object, so really I will have the object two instances for the object in 
> memory ...
Only until the garbage collection cleans up the "first" instance. So you
_always_ create one instance for the garbage collector only.
> 
> 2) In case B, PHP creates a noew object and returns a reference to this 
> newly created object. In this case there is only one instance of the object.
> 
> Does it works that way? If not, What is exactly the difference?
It is as you say. The problem is normally negligible, but makes a big
difference if there is alot done in the constructor of the object (e.g.
scanning a huge file for certain strings). Then this will slow things
down. This however is solved in PHP5.
> 
> TIA,
> Jordi.
Cheerio
/rudy


signature.asc
Description: This is a digitally signed message part


[PHP] PHP5 and static attributes

2004-05-12 Thread Rudy Metzger
Dear all,

I have a problem with 'referencing' static attributes. I have the
following class tree.

// --- CLASS A -
class A
{
  protected static $myInstance;
}

// --- CLASS B --
class B extends class A
{
}

// --- CLASS C --
class C extends class B
{

public function Debug()
{
  echo self::$myInstance; // does not work (undefined)
  echo parent::$myInstance;  // also does not work
  echo A::$myInstance; // works
}

}

-
The Problem is that you always have to know in which class the static
was defined to reference it. Or is there something like
static::$myInstance or this::$myInstance or class::$myInstance. If not,
it maybe would be a great idea to add something to PHP, otherwise you
always have to track in which class the static had been defined if you
want to reference it!

Thanx for reading and if someone knows how to solve this, please let me
know!

Cheerio
/rudy

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php