simple question about OO in PHP

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
beentheredonethat
Forum Newbie
Posts: 1
Joined: Wed Aug 12, 2009 2:47 am

simple question about OO in PHP

Post 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;
}
 
User avatar
dheeraj
Forum Commoner
Posts: 40
Joined: Fri Feb 06, 2009 11:54 am

Re: simple question about OO in PHP

Post 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;
 
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: simple question about OO in PHP

Post 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
Post Reply