Problem with classes - new instance failing within a class definition

Problem with classes - new instance failing within a class definition

Post by Peter Chan » Tue, 23 Jun 2009 16:19:55


Chaps,

I've been using php for a while but have not tried classes before. No too
familiar with object orientated programming. I may have made a simple
mistake.

Code:

class ODF_Substitute {
var $ODF_filename;
var $ODF_ziparc = new ZipArchive();
...

With the third line commented out all is fine, with it in place I get an
error message.

Parse error: syntax error, unexpected T_NEW...

Is there some limitation that I can't use an instance of a class within
another class? In this case I don't think extending ZipArchive makes
sense.

Pete

--
http://www.yqcomputer.com/
 
 
 

Problem with classes - new instance failing within a class definition

Post by Willem Bog » Tue, 23 Jun 2009 16:31:35

> class ODF_Substitute {

I think you can use only "constant" expressions here. You can set the
initial value in the constructor. By the way, are you still using PHP4?
If not, use the scope identifiers (private, protected or public) instead
of the var keyword.

Best regards,
--
Willem Bogaerts

Application smith
Kratz B.V.
http://www.yqcomputer.com/

 
 
 

Problem with classes - new instance failing within a class definition

Post by varo G. Vi » Tue, 23 Jun 2009 16:58:15

Peter Chant escribi >> I've been using php for a while but have not tried classes before. No too >> familiar with object orientated programming. I may have made a simple >> mistake. >> >> Code: >> >> class ODF_Substitute { >> var $ODF_filename; >> var $ODF_ziparc = new ZipArchive(); >> ... >> >> With the third line commented out all is fine, with it in place I get an >> error message. >> >> Parse error: syntax error, unexpected T_NEW... >> >> Is there some limitation that I can't use an instance of a class within >> another class? In this case I don't think extending ZipArchive makes >> sense.

That kind of stuff normally goes in a constructor. The reference is here:

PHP/5 and later: http://www.yqcomputer.com/
PHP/4: http://www.yqcomputer.com/




--
-- http://www.yqcomputer.com/ - varo G. Vicario - Burgos, Spain
-- Mi sitio sobre programaci web: http://www.yqcomputer.com/
-- Mi web de humor satinado: http://www.yqcomputer.com/
 
 
 

Problem with classes - new instance failing within a class definition

Post by Gordo » Tue, 23 Jun 2009 17:26:32

On Jun 22, 8:19m, Peter Chant < XXXX@XXXXX.COM >

> var $ODF_filename;> > var $ODF_ziparc = new ZipArchive(>;
> gt;>..
>
> With the third line commented out all is fine, with it in place I >et an
> error me>sa>e.
>
> Parse error: syntax error, unexpected T_>EW>..
>
> Is there some limitation that I can't use an instance of a class >ithin
> another class? n this case I don't think extending ZipArchive>makes
> >en>e.
>
>
> -- http://www.yqcomputer.com/

You can only initialize variables in classes with literals. If you
need them to take the result of a function/method call then you need
to put it in the constructor method.
 
 
 

Problem with classes - new instance failing within a class definition

Post by Peter Chan » Wed, 24 Jun 2009 03:20:04


5.2.3

Noted the comments on private, protected and public. Never got quite used
to the idea of object orientated programming from what I read up. Always
got lost when inheritance was mentioned. I think the penny is beginning to
drop. From reading up on C++ years back can see the value in keeping
variables private unless there is a good reason not to. Interestingly
nearly all examples I have found so far have declared all variables
public.k

Compare and contrast with Python where all variables are public but you put
an underscore in front so that, by gentlemans agreement, you treat it as
private. Not trying to start a flamewar by the way, just an observation.

Pete

--
http://www.yqcomputer.com/
 
 
 

Problem with classes - new instance failing within a class definition

Post by Peter Chan » Wed, 24 Jun 2009 03:20:53

 
 
 

Problem with classes - new instance failing within a class definition

Post by Peter Chan » Wed, 24 Jun 2009 03:21:42


Thanks. Also helps when you learn that you can't get away with the
spelling "_contruct"!

--
http://www.yqcomputer.com/