Accessing Object Properties with a Const value?

Associate
Joined
4 Mar 2007
Posts
315
Location
United Kingdom
I was curious suppose I have an abstract class in PHP such like:

Code:
define("SOME_CONST",'thisisaconst');

abstract class foo {

 public function myMethod() {
  return (object) array (
    SOME_CONST => 12
  )
 }

}

class bar extends foo{

 public function getsomething() {
   $someObject = $this->myMethod();
   $someObject->SOME_CONST; // This isn't possible.
 }

}

But is there a way in which I can refer to $someObject->SOME_CONST without having to call the SOME_CONST defined value?

Regards,
 
I'm no expert in php but the way this is done in other languages is with a static variable in the class. i.e. a variable associated with the class but not tied to a particular instance.
 
Edit: Oh, ignore me, I didn't read the question correctly.

I think it should be like:

Code:
abstract class Foo {
  
  const SOME_CONST = 12;

  public function myMethod() {
    return array(self::SOME_CONST);
  }

}

class Bar extends Foo {
  
  public function getSomething() {
    self::SOME_CONST; // I think this works but if not...
    Foo::SOME_CONST; // This will.
  }
  
}
 
Last edited:
Back
Top Bottom