Accessing PHP static variables for multiple class files

I have a PHP class file A with a static variable, I have one class file B that uses this class A and creates its static variable. I want to access the varicale class of class A from another class C in another file. How should I do it. I tried this. I know the singleton structure, but not sure if this is relevant to this. see below what I'm trying to achieve. Please, I don't want to use sessions.

classA.php

  <?php
   Class A{
  public static $foo = array ();
 public static function doSomething(){
   //... Do some process and instantiate static vairable $face
   self::$foo = outputofsomeprocess();
 }
}
?>

      

classB.php

<?php
require_once "classA.php";
class B{
public function dosomethingelse(){
    A::doSomething();
}
}
?>

      

classC.php

<?php
require_once "classA.php";
 class C{
    public function dosomethingelse(){
    echo A::$foo[0];
   }
  }
?>

      

I am getting null from echo in class C

+3


source to share


1 answer


Coming from a C # background, this is slightly different, but that should do the trick.



class A1 {

    public static $foo = array();

}


<?php
require_once 'classA.php';

class B2 {

    public static function doSomethings() {

       var_dump(A1::$foo);
    }
}

B2::doSomethings();

      

0


source







All Articles