You are not logged in.

#1 2005-02-19 10:20:07

cactus
Taco Eater
From: t͈̫̹ͨa͖͕͎̱͈ͨ͆ć̥̖̝o̫̫̼s͈̭̱̞͍̃!̰
Registered: 2004-05-25
Posts: 4,622
Website

php5 programming question..

to all you php5 gurus out there.  wink

This is what I want to do:

class Test {
  private static $var;
  public static function Init() {
    self::$var=new SomeClass();
  }

  public static function Eat() {
    self::$var->Eat();
  }
}

Test::Init();
Test::Eat();

It is no worky.  :?
In fact, it causes a seg fault in one of the httpd daemon child processes. :shock:

[notice] child pid 3507 exit signal Segmentation fault (11)

I could try using a singleton, but I don't see why I should. Is this a shortcoming of the php5 class engine, or am I just doing it wrong?
Anyone?


"Be conservative in what you send; be liberal in what you accept." -- Postel's Law
"tacos" -- Cactus' Law
"t̥͍͎̪̪͗a̴̻̩͈͚ͨc̠o̩̙͈ͫͅs͙͎̙͊ ͔͇̫̜t͎̳̀a̜̞̗ͩc̗͍͚o̲̯̿s̖̣̤̙͌ ̖̜̈ț̰̫͓ạ̪͖̳c̲͎͕̰̯̃̈o͉ͅs̪ͪ ̜̻̖̜͕" -- -̖͚̫̙̓-̺̠͇ͤ̃ ̜̪̜ͯZ͔̗̭̞ͪA̝͈̙͖̩L͉̠̺͓G̙̞̦͖O̳̗͍

Offline

#2 2005-03-07 19:14:12

soloport
Member
Registered: 2005-03-01
Posts: 442

Re: php5 programming question..

Well, you don't offer but a hint as to what's in "SomeClass", so I rolled my own.  This works for me (no segfault):

$cat > test.php
<?php
class SomeClass {
    public static function Eat() {
        echo "Testn";
    }
}
class Test {
    private static $var;
    public static function Init() {
        self::$var=new SomeClass();
    }

    public static function Eat() {
        self::$var->Eat();
    }
}

Test::Init();
Test::Eat();
?>
^D

Output:

$ php test.php
Test

Also, why not use "function __construct()" instead of calling Init()?

Offline

#3 2005-03-07 21:24:08

cactus
Taco Eater
From: t͈̫̹ͨa͖͕͎̱͈ͨ͆ć̥̖̝o̫̫̼s͈̭̱̞͍̃!̰
Registered: 2004-05-25
Posts: 4,622
Website

Re: php5 programming question..

heh..thanks. I figured out my issue quite a while ago. I forgot to post that I had solved it on my own..
thanks though..

as for why no __construct? because it is a static class..it is never instantiated, just called. Hence, a constructor seemed silly..besides, you should avoid  initializing other objects in a constructor. If a constructor fails while doing an object initialization, it could cause a race condition..


"Be conservative in what you send; be liberal in what you accept." -- Postel's Law
"tacos" -- Cactus' Law
"t̥͍͎̪̪͗a̴̻̩͈͚ͨc̠o̩̙͈ͫͅs͙͎̙͊ ͔͇̫̜t͎̳̀a̜̞̗ͩc̗͍͚o̲̯̿s̖̣̤̙͌ ̖̜̈ț̰̫͓ạ̪͖̳c̲͎͕̰̯̃̈o͉ͅs̪ͪ ̜̻̖̜͕" -- -̖͚̫̙̓-̺̠͇ͤ̃ ̜̪̜ͯZ͔̗̭̞ͪA̝͈̙͖̩L͉̠̺͓G̙̞̦͖O̳̗͍

Offline

Board footer

Powered by FluxBB