Page 1 of 1

simple question about OO in PHP

Posted: Wed Aug 12, 2009 2:55 am
by beentheredonethat
Hello everyone,
I'm new to PHP and used to code in Java. I tried the following but get a parser error for the "public $bla =..." line. X_Debug tells me: "Syntax Error: Expected: identifier, +, -". What is wrong?

Interestingly, I don't get an error message if I move the line beyond the class definition.

Code: Select all

 
class Coordinate2D
{
    public $x = 0;
    public $y = 0;
}
 
class Paper
{
    // paper dimension
    const WIDTH_MM = 380;
    const HEIGHT_MM = 290;
 
    // 1 point = 2.83465669 mm
    const MM_PER_POINT = 2.83465669;
 
    public $bla = new Coordinate2D;
}
 

Re: simple question about OO in PHP

Posted: Wed Aug 12, 2009 3:27 am
by dheeraj
i think in php u can't use const for declaring constant....

define() is used to declare it, like below...

Code: Select all

define("PAGE_SIZE","100");
echo PAGE_SIZE;
 

Re: simple question about OO in PHP

Posted: Wed Aug 12, 2009 3:32 am
by Eran
that's incorrect, defining constants in classes are done exactly as he's shown. You are using global constants, which are not related to OOP.

The problem is you can't use the 'new' keyword in a member definition. If you want to assign a new object instance to that member you'll have to do it in the constructor.

By the way, the error you should be seeing is something like "Parse error: syntax error, unexpected T_NEW in .. on line .." . please post the full error message in the future to make it easier for us to help you