Author Topic: [SOLVED] New to OOP need a bit of help.  (Read 169 times)

0 Members and 1 Guest are viewing this topic.

Offline unkwntechTopic starter

  • Enthusiast
  • Posts: 444
  • Gender: Male
  • $me->selfDestruct('NOW');
    • View Profile
    • UnkwnDesign.Com
[SOLVED] New to OOP need a bit of help.
« on: November 30, 2008, 04:09:23 AM »
The following code snip is giving me an error in PHP5:

<?php
class myClass
 
{
	
public 
$myVar$self->myFunction(); //Error here
        
function myFunction()
         {
                 return 
'Hello World';
         }
 }
$data = new myClass;
echo 
$data->myVar;
?>


The error is "Unexpected T_VARIABLE".
What am I doing wrong, and is there anything else I should be aware of?
« Last Edit: November 30, 2008, 04:14:35 AM by unkwntech »
<-- Disclaimer:  I am a freelance PHP developer. Freelance does not mean that my work is free.  If I help that means I took time away from making money, please don't be grumpy.  And I may or may not have tested my code, if I posted it then most likely my IDE said the syntax was correct however that does not mean that it works. -->
If you'd like to buy my services see: unkwndesign.com
UGG.. regex... cant live with it, cant live without it. :)

Offline Mchl

  • Staff Alumni
  • Freak!
  • *
  • Posts: 8,582
  • Gender: Male
  • That's Largo in my avatar, not me.
    • View Profile
    • FlingBits
Re: New to OOP need a bit of help.
« Reply #1 on: November 30, 2008, 05:06:34 AM »
You can't assign a value to a class variable using methods or funstions.
YOu have to move this to __construct() method


<?php
class myClass
 
{

	


public 
$myVar //Error here

        
public function __construct()
        {
        
$this->myVar $this->myFunction();
        }

        function 
myFunction()
         {
                 return 
'Hello World';
         }
 }
$data = new myClass;
echo 
$data->myVar;
?>
NetBeans fanatic | ExtJS masochist | C++ denier
PHP4 & MySQL4 are no longer supported.
PHPFreaks Tutorials | PHP Debugging: A Beginner's guide | PHP Security Tutorial || How To Ask Questions The Smart Way
Flingbits tutorials | Class Autoloading