Current location: Hot Scripts Forums » Programming Languages » PHP » Newb PHP Class


Newb PHP Class

Reply
  #1 (permalink)  
Old 07-15-10, 03:59 PM
greenwaldWeb greenwaldWeb is offline
Newbie Coder
 
Join Date: Aug 2009
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Newb PHP Class

What's wrong with this?
PHP Code:

<?php
class Test {
  private 
$str 'a';
  public function 
readStr() { 
    echo 
$this->$str;
  }
}
$testStr = new Test;
$testStr->readStr();
?>
It throws an undefined variable error... thanks for helping a new guy out.

Last edited by job0107; 07-15-10 at 04:16 PM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #2 (permalink)  
Old 07-15-10, 04:16 PM
job0107's Avatar
job0107 job0107 is offline
Community Liaison
 
Join Date: Dec 2006
Location: Tacoma, Washington USA
Posts: 3,454
Thanks: 0
Thanked 140 Times in 137 Posts
After you declare a variable in the CLASS, you don't use the $ with the variable name to reference it.

Example:
PHP Code:

<?php
class Test {
public 
$str 'a';
public function 
readStr() {
echo 
$this->str;
}
}
$testStr = new Test;
$testStr->readStr();
echo 
"<br />";
$testStr->str "Hello World";
$testStr->readStr();
?>
__________________
Jerry Broughton
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #3 (permalink)  
Old 07-15-10, 04:18 PM
greenwaldWeb greenwaldWeb is offline
Newbie Coder
 
Join Date: Aug 2009
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Much appreciated
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #4 (permalink)  
Old 07-15-10, 04:26 PM
job0107's Avatar
job0107 job0107 is offline
Community Liaison
 
Join Date: Dec 2006
Location: Tacoma, Washington USA
Posts: 3,454
Thanks: 0
Thanked 140 Times in 137 Posts
Your welcome.
__________________
Jerry Broughton
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
SMS Instant Messaging PHP Class carters-site PHP 2 01-20-10 01:50 PM
PHP - class/ basic question SurfboYne PHP 7 01-20-08 02:56 PM
PHP global variables don't work inside a class wizkid PHP 4 12-20-04 04:31 PM
Need Help In PHP Programming for a single code suggestion !! CyberRomeo PHP 1 11-27-04 11:38 AM


All times are GMT -5. The time now is 01:16 PM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.