0

I know you don`t have to specify type for PHP variables, but can this be done ? No initializing, just type casting.

class A {
    ....
}

class B {

  public var foo= A;

}

Thank You !

Ish
  • 1
  • No. And it's a constant identifier in that context anyway, not a class reference. See also: [Attribute declarations in a class definition can only be constant values, not expressions.](http://stackoverflow.com/questions/2671928/workaround-for-basic-syntax-not-being-parsed) – mario Aug 27 '12 at 17:23

2 Answers2

0

are you looking for the settype() PHP function?

Najzero
  • 3,164
  • 18
  • 18
  • id doesn`t allow any other types than "boolean" (or, since PHP 4.2.0, "bool") "integer" (or, since PHP 4.2.0, "int") "float" (only possible since PHP 4.2.0, for older versions use the deprecated variant "double") "string" "array" "object" "null" (since PHP 4.2.0) – Ish Aug 27 '12 at 17:31
  • besides, that will have to be used in a method, even if __construct() ... which would defeat the purpose a little – Ish Aug 27 '12 at 17:36
0

You can do this (string) $a; Its force the code recive this type.

Guerra
  • 2,792
  • 1
  • 22
  • 32
  • if i try to do "public (A) foo;" it throws "Parse error: syntax error, unexpected '(', expecting T_VARIABLE" – Ish Aug 27 '12 at 17:28
  • Look that http://stackoverflow.com/questions/390932/declaring-variable-types-in-php – Guerra Aug 27 '12 at 17:33
  • Thanks, i`ve actually scanned through the answer before, but it seemed to be something different than what i was asking ... back to commenting the var type after the declaration :) .... – Ish Aug 27 '12 at 17:47