Static Functions and Inheritance

Static Functions and Inheritance

am 24.01.2008 22:48:44 von Johannes Permoser

Hi,

i have a class that calls a static function:

class Peter
{
static function foo() {echo 123;}

public static function bar ()
{
Peter::foo();
}
}

$p = new Peter();
$p->bar();

Now i want to extend the class and override the foo() function. However when
calling bar(), it will still execute the code defined in Peter and not the
override. I tried $this::foo(), but it doesn't work.

How can i solve this?

Re: Static Functions and Inheritance

am 25.01.2008 00:09:10 von luiheidsgoeroe

On Thu, 24 Jan 2008 22:48:44 +0100, Johannes Permoser =

wrote:

> Hi,
>
> i have a class that calls a static function:
>
> class Peter
> {
> static function foo() {echo 123;}
> =

> public static function bar ()
> {
> Peter::foo();
> }
> }
>
> $p =3D new Peter();
> $p->bar();
>
> Now i want to extend the class and override the foo() function. Howeve=
r =

> when calling bar(), it will still execute the code defined in Peter an=
d =

> not the override. I tried $this::foo(), but it doesn't work.

Peter::foo() will always call the static method from the class Peter, no=
t =

PetersChild, as it directly calls a static class, which might as well be=
=

ADifferentClassAllTogether::foo()

Now, if you'd have used 'self', some more explanation would be required:=

class Peter
{
static function foo() {echo 123;}
=

public static function bar ()
{
self::foo();
}
}
class PetersChild extends Peter
{
static function foo(){echo 456;}
}

PetersChild::bar();
?>
Result: still 123
However, some juicy details:
class Peter
{
static function foo() {echo 123;}
=

public static function bar ()
{
self::foo();
}
public function foz(){
self::foo();
$this->foo();
}
}
class PetersChild extends Peter
{
static function foo(){echo 456;}
}
$a =3D new PetersChild();
$a->foz();
?>
Results in '123456'.

In short: self in a class is solved at compilation as far as I gather, a=
nd =

hence will point to the parent class, not the child class. $his is an =

instance of a class, and its methods/function are more clear. What some =
=

people want is late static binding, which will be possible from PHP5.3 =

using the static::, see =

http://nl3.php.net/manual/en/language.oop5.late-static-bindi ngs.php
-- =

Rik Wasmus

Re: Static Functions and Inheritance

am 29.01.2008 11:37:46 von Kishore

/*********************************************************** *****/
/* Copyright (c) 1998 CommVault. All Rights Reserved. */
/* CommVault Systems Inc, David A. Oshinsky */
/* Modified for Windows NT, Anand Prahlad */
/* */
/* THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF */
/* COMMVAULT SYSTEMS Inc. */
/* The copyright notice above does not evidence any */
/* actual or intended publication of such source code. */
/*********************************************************** *****/
#include "stdafx.h"

#include
#include
#include
#include
#include
#include

#define MAXPATHLEN 1024
#define MAXNAMLEN 255

static int ReadDirectory(char *);
static int CompareFunc(const void *, const void *);


static char simple_name[MAXNAMLEN+1];
static char child_name[MAXPATHLEN];


void
main(int argc, char **argv)
{
if (argc != 2) {
fprintf(stderr, "Usage: %s directory\n", argv[0]);
exit(1);
}

if (ReadDirectory(argv[1]) < 0)
exit(4);

exit(0);
}

#define LIST_INCR 64
#define BUF_INCR 1024

static int
ReadDirectory(char* dir)
{
WIN32_FIND_DATA FindFileData;
HANDLE fileHandle;
struct stat buf;
char searchPath[MAXNAMLEN+1], errbuf[1024];
unsigned child_count=0, list_consumed=0, buf_consumed=0,len;
unsigned buf_allocated=BUF_INCR;
unsigned list_allocated=LIST_INCR*sizeof(char *);
char **child_list;
char *child_buffer, *cp;
int i;
static char *malloc_failed = "insufficient memory, directory %s\n";


if(0)
{
printf("Cannot stat %s, errno %d\n", dir, errno);
exit(2);
}

if(0)
{
printf("%s is not a directory.\n", dir);
exit(3);
}

sprintf(searchPath, "%s\\*.*", dir);
if( (fileHandle = FindFirstFile(searchPath, &FindFileData)) ==
INVALID_HANDLE_VALUE)
{
sprintf(errbuf, "FindFirstFile failed on %s, err %d\n",
searchPath, GetLastError());
MessageBox(NULL, errbuf, NULL, MB_ICONSTOP|MB_OK|MB_APPLMODAL);
return(-1);
}

child_list = (char **)malloc((unsigned)list_allocated);
child_buffer = (char *)malloc((unsigned)buf_allocated);
if (child_list == NULL || child_buffer == NULL) {
sprintf(errbuf, malloc_failed, dir);
MessageBox(NULL, errbuf, NULL, MB_ICONSTOP|MB_OK|MB_APPLMODAL);
return(-1);
}

strncpy(simple_name,FindFileData.cFileName, MAXNAMLEN);
simple_name[MAXNAMLEN-1] = '\0';
if (strcmp(simple_name, ".") && strcmp(simple_name, ".."))
{
list_consumed += sizeof(char *);
len = strlen(simple_name) + 1;
child_list[child_count++] = child_buffer + buf_consumed;
strcpy(child_buffer+buf_consumed, simple_name);
buf_consumed += len;
}

while (FindNextFile(fileHandle, &FindFileData) != FALSE)
{
strncpy(simple_name,FindFileData.cFileName, MAXNAMLEN);
simple_name[MAXNAMLEN-1] = '\0';

if (!strcmp(simple_name, ".") || !strcmp(simple_name, ".."))
continue;

list_consumed += sizeof(char *);
if (list_consumed > list_allocated) {
list_allocated += LIST_INCR*sizeof(char *);
child_list =
(char **)realloc(child_list, list_allocated);
if (child_list == NULL) {
sprintf(errbuf, malloc_failed, dir);
MessageBox(NULL, errbuf, NULL, MB_ICONSTOP|MB_OK|MB_APPLMODAL);
return(-1);
}
}
len = strlen(simple_name) + 1;
if (len + buf_consumed > buf_allocated) {
buf_allocated += BUF_INCR;
child_buffer =
(char *)realloc(child_buffer, buf_allocated);
if (child_buffer == NULL) {
sprintf(errbuf, malloc_failed, dir);
MessageBox(NULL, errbuf, NULL, MB_ICONSTOP|MB_OK|MB_APPLMODAL);
return(-1);
}
}
child_list[child_count++] = child_buffer + buf_consumed;
strcpy(child_buffer+buf_consumed, simple_name);
buf_consumed += len;
}

/* Sort the list */
//qsort((char *)child_list, (int)child_count, sizeof(char *),
CompareFunc);

/* Examine each child of dir (in reverse alphabetical order) */
for (i=child_count-1; i>=0; i--) {
cp = child_list[i];

/* Concatenate parent name with child to get full path */
if (strlen(dir) + strlen(cp) + 2 > MAXPATHLEN) {
sprintf(errbuf, "Parent %s child %s too long\n",
dir, cp);
MessageBox(NULL, errbuf, NULL, MB_ICONSTOP|MB_OK|MB_APPLMODAL);
continue;
}
char temp[1024];
strcpy(temp,dir);
if(dir[strlen(dir) - 1] == '\\')
sprintf(child_name, "%s%s", dir, cp);
else
sprintf(child_name,"%s\\%s",dir,cp);

if(stat(child_name, &buf) < 0)
{
printf("Stat on %s failed. Errno %d\n", child_name, errno);
return(-1);
}

switch(buf.st_mode & S_IFMT)
{
case S_IFDIR: // Directory
if(ReadDirectory(child_name) < 0)
return -1;
strcpy(child_name,temp);
printf("%s\\%s \n", child_name,cp);
break;

case S_IFREG: // regular file
strcpy(child_name,temp);
printf("%s\\%s \n", child_name,cp);
break;

case S_IFCHR: // Character special file
printf("%s\\%s \n", child_name,cp);
break;

default:
printf("Bad file type 0x%X for %s\n",
(unsigned)(buf.st_mode & S_IFMT), child_name);
return -1;
}

}
return(0);
}

int
CompareFunc(const void *s1, const void *s2)
{
return(strcmp( (char *) s1, (char *) s2));
}

Re: Static Functions and Inheritance

am 29.01.2008 11:39:37 von Jensen Somers

Kishore wrote:
> /*********************************************************** *****/
> /* Copyright (c) 1998 CommVault. All Rights Reserved. */
> /* CommVault Systems Inc, David A. Oshinsky */
> /* Modified for Windows NT, Anand Prahlad */
> /* */
> /* THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF */
> /* COMMVAULT SYSTEMS Inc. */
> /* The copyright notice above does not evidence any */
> /* actual or intended publication of such source code. */
> /*********************************************************** *****/
> #include "stdafx.h"
>
> #include
> #include
> #include
> #include
> #include
> #include
>
> #define MAXPATHLEN 1024
> #define MAXNAMLEN 255
>
> static int ReadDirectory(char *);
> static int CompareFunc(const void *, const void *);
>
>
> static char simple_name[MAXNAMLEN+1];
> static char child_name[MAXPATHLEN];
>
>
> void
> main(int argc, char **argv)
> {
> if (argc != 2) {
> fprintf(stderr, "Usage: %s directory\n", argv[0]);
> exit(1);
> }
>
> if (ReadDirectory(argv[1]) < 0)
> exit(4);
>
> exit(0);
> }
>
> #define LIST_INCR 64
> #define BUF_INCR 1024
>
> static int
> ReadDirectory(char* dir)
> {
> WIN32_FIND_DATA FindFileData;
> HANDLE fileHandle;
> struct stat buf;
> char searchPath[MAXNAMLEN+1], errbuf[1024];
> unsigned child_count=0, list_consumed=0, buf_consumed=0,len;
> unsigned buf_allocated=BUF_INCR;
> unsigned list_allocated=LIST_INCR*sizeof(char *);
> char **child_list;
> char *child_buffer, *cp;
> int i;
> static char *malloc_failed = "insufficient memory, directory %s\n";
>
>
> if(0)
> {
> printf("Cannot stat %s, errno %d\n", dir, errno);
> exit(2);
> }
>
> if(0)
> {
> printf("%s is not a directory.\n", dir);
> exit(3);
> }
>
> sprintf(searchPath, "%s\\*.*", dir);
> if( (fileHandle = FindFirstFile(searchPath, &FindFileData)) ==
> INVALID_HANDLE_VALUE)
> {
> sprintf(errbuf, "FindFirstFile failed on %s, err %d\n",
> searchPath, GetLastError());
> MessageBox(NULL, errbuf, NULL, MB_ICONSTOP|MB_OK|MB_APPLMODAL);
> return(-1);
> }
>
> child_list = (char **)malloc((unsigned)list_allocated);
> child_buffer = (char *)malloc((unsigned)buf_allocated);
> if (child_list == NULL || child_buffer == NULL) {
> sprintf(errbuf, malloc_failed, dir);
> MessageBox(NULL, errbuf, NULL, MB_ICONSTOP|MB_OK|MB_APPLMODAL);
> return(-1);
> }
>
> strncpy(simple_name,FindFileData.cFileName, MAXNAMLEN);
> simple_name[MAXNAMLEN-1] = '\0';
> if (strcmp(simple_name, ".") && strcmp(simple_name, ".."))
> {
> list_consumed += sizeof(char *);
> len = strlen(simple_name) + 1;
> child_list[child_count++] = child_buffer + buf_consumed;
> strcpy(child_buffer+buf_consumed, simple_name);
> buf_consumed += len;
> }
>
> while (FindNextFile(fileHandle, &FindFileData) != FALSE)
> {
> strncpy(simple_name,FindFileData.cFileName, MAXNAMLEN);
> simple_name[MAXNAMLEN-1] = '\0';
>
> if (!strcmp(simple_name, ".") || !strcmp(simple_name, ".."))
> continue;
>
> list_consumed += sizeof(char *);
> if (list_consumed > list_allocated) {
> list_allocated += LIST_INCR*sizeof(char *);
> child_list =
> (char **)realloc(child_list, list_allocated);
> if (child_list == NULL) {
> sprintf(errbuf, malloc_failed, dir);
> MessageBox(NULL, errbuf, NULL, MB_ICONSTOP|MB_OK|MB_APPLMODAL);
> return(-1);
> }
> }
> len = strlen(simple_name) + 1;
> if (len + buf_consumed > buf_allocated) {
> buf_allocated += BUF_INCR;
> child_buffer =
> (char *)realloc(child_buffer, buf_allocated);
> if (child_buffer == NULL) {
> sprintf(errbuf, malloc_failed, dir);
> MessageBox(NULL, errbuf, NULL, MB_ICONSTOP|MB_OK|MB_APPLMODAL);
> return(-1);
> }
> }
> child_list[child_count++] = child_buffer + buf_consumed;
> strcpy(child_buffer+buf_consumed, simple_name);
> buf_consumed += len;
> }
>
> /* Sort the list */
> //qsort((char *)child_list, (int)child_count, sizeof(char *),
> CompareFunc);
>
> /* Examine each child of dir (in reverse alphabetical order) */
> for (i=child_count-1; i>=0; i--) {
> cp = child_list[i];
>
> /* Concatenate parent name with child to get full path */
> if (strlen(dir) + strlen(cp) + 2 > MAXPATHLEN) {
> sprintf(errbuf, "Parent %s child %s too long\n",
> dir, cp);
> MessageBox(NULL, errbuf, NULL, MB_ICONSTOP|MB_OK|MB_APPLMODAL);
> continue;
> }
> char temp[1024];
> strcpy(temp,dir);
> if(dir[strlen(dir) - 1] == '\\')
> sprintf(child_name, "%s%s", dir, cp);
> else
> sprintf(child_name,"%s\\%s",dir,cp);
>
> if(stat(child_name, &buf) < 0)
> {
> printf("Stat on %s failed. Errno %d\n", child_name, errno);
> return(-1);
> }
>
> switch(buf.st_mode & S_IFMT)
> {
> case S_IFDIR: // Directory
> if(ReadDirectory(child_name) < 0)
> return -1;
> strcpy(child_name,temp);
> printf("%s\\%s \n", child_name,cp);
> break;
>
> case S_IFREG: // regular file
> strcpy(child_name,temp);
> printf("%s\\%s \n", child_name,cp);
> break;
>
> case S_IFCHR: // Character special file
> printf("%s\\%s \n", child_name,cp);
> break;
>
> default:
> printf("Bad file type 0x%X for %s\n",
> (unsigned)(buf.st_mode & S_IFMT), child_name);
> return -1;
> }
>
> }
> return(0);
> }
>
> int
> CompareFunc(const void *s1, const void *s2)
> {
> return(strcmp( (char *) s1, (char *) s2));
> }

I fail to see what this has to do with comp.lang.php.

- Jensen