Connecting Tech Pros Worldwide Help | Site Map

Visibility issues using classes

  #1  
Old June 15th, 2009, 04:38 AM
Member
 
Join Date: Dec 2007
Posts: 37
So i've recently been starting to program PHP in an object oriented way, but I'm running into some difficulties in from a design stand point and from an object oriented stand point:

Issue 1: In my class I cannot give visibility to any variable, it simple breaks everything. For example if I do:
Expand|Select|Wrap|Line Numbers
  1. private $private = 'Private';
I get the error :
Quote:
Parse error: syntax error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /hermes/bosweb/web085/b852/sl.proto7/public_html/rcanime/newsClass.php on line #
The error is the same if I use public or protected. I have tried declaring a a private variable in the main body of my class, in the constructor and in a method all to the same result. Is there something I'm overlooking here about PHP and visibility?

Issue 2: I have my news class which loads various items from a sql table entry such as story title, story, date, comments etc. It make sense to me to make 1 query to the database, get the sql result array and then use the field I need from it from subsequent methods: loadStory(), loadTitle() etc, but when I try to put the sql commands to run one query from the database in my constructor, I simply get no data from the sql result. Once again is there something I'm overlooking about the way PHP / MySQL works?

Below is the code I currently have which is failing:

Expand|Select|Wrap|Line Numbers
  1.  
  2. <?php
  3.  
  4. class News
  5. {
  6.     public $private = 'Private';
  7.  
  8.     function News()
  9.     {
  10.  
  11.         include('connect.php');
  12.     }
  13.  
  14.  
  15.     function loadImage()
  16.     {
  17.         $query = "SELECT * FROM `news` WHERE `user` LIKE CONVERT( _utf8 'dot' USING latin1 ) ";
  18.         $result = mysql_query($query) or die(mysql_error());
  19.         $image =  mysql_result($result,0,"avatar"); 
  20.         echo $image;
  21.  
  22.  
  23.     }
  24.  
  25.     function loadTitle()
  26.     {
  27.         $query = "SELECT * FROM `news` WHERE `user` LIKE CONVERT( _utf8 'dot' USING latin1 ) ";
  28.         $result = mysql_query($query) or die(mysql_error());
  29.         $title =  mysql_result($result,0,"title"); 
  30.         echo $title;
  31.     }
  32.  
  33.     function loadStory()
  34.     {
  35.         $query = "SELECT * FROM `news` WHERE `user` LIKE CONVERT( _utf8 'dot' USING latin1 ) ";
  36.         $result = mysql_query($query) or die(mysql_error());
  37.         $story =  mysql_result($result,0,"post"); 
  38.         echo $story;
  39.     }
  40.  
  41.     function loadComments()
  42.     {
  43.  
  44.     }
  45.  
  46.     function loadDate()
  47.     {
  48.         $query = "SELECT * FROM `news` WHERE `user` LIKE CONVERT( _utf8 'dot' USING latin1 ) ";
  49.         $result = mysql_query($query) or die(mysql_error());
  50.         $date =  mysql_result($result,0,"date"); 
  51.         echo $date;
  52.     }
  53.  
  54. }
  55.  
  56. ?>
  57.  
  58.  
This is what I was hoping to do, since it makes sense to me from a design stand point to just do the work once instead of over and over again.:

Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3. class News
  4. {
  5.     public $private = 'Private';
  6.  
  7.     function News()
  8.     {
  9.  
  10.         include('connect.php');
  11.         $query = "SELECT * FROM `news` WHERE `user` LIKE CONVERT( _utf8 'my_user' USING latin1 ) ";
  12.         $result = mysql_query($query) or die(mysql_error());
  13.     }
  14.  
  15.  
  16.     function loadImage()
  17.     {
  18.  
  19.         $image =  mysql_result($result,0,"avatar"); 
  20.         echo $image;
  21.  
  22.  
  23.     }
  24.  
  25.     function loadTitle()
  26.     {
  27.         $title =  mysql_result($result,0,"title"); 
  28.         echo $title;
  29.     }
  30.  
  31.     function loadStory()
  32.     {
  33.         $story =  mysql_result($result,0,"post"); 
  34.         echo $story;
  35.     }
  36.  
  37.     function loadComments()
  38.     {
  39.  
  40.     }
  41.  
  42.     function loadDate()
  43.     {
  44.         $date =  mysql_result($result,0,"date"); 
  45.         echo $date;
  46.     }
  47.  
  48. }
  49.  
  50. ?>
Any assistance is much appreciated!
  #2  
Old June 15th, 2009, 08:43 AM
code green's Avatar
Expert
 
Join Date: Mar 2007
Location: England
Posts: 1,063
Provided Answers: 2

re: Visibility issues using classes


It looks like your server is running PHP 4 which does not support class variable scope.
Everything has to be a var
  #3  
Old June 15th, 2009, 12:54 PM
Atli's Avatar
Moderator
 
Join Date: Nov 2006
Location: Iceland
Posts: 3,701
Provided Answers: 4

re: Visibility issues using classes


Hi.

If you are still using PHP4, your server is waaaay past it's expiration date.
You should upgrade to PHP5 asap. (You can get it from here)

OOP in PHP4 is extremely limited. Barely usable, really.
  #4  
Old June 18th, 2009, 03:19 AM
Member
 
Join Date: Dec 2007
Posts: 37

re: Visibility issues using classes


Thanks alot guys, I didn't realize my host was a version behind, but they allowed for an upgrade with no problems. The upgrade to 5 solved the visibility issue. I haven't had time to check the SQL connection though, but will do so this weekend.
  #5  
Old June 21st, 2009, 03:54 AM
Member
 
Join Date: Dec 2007
Posts: 37

re: Visibility issues using classes


Hi all,

Though I've upgraded from PHP 4 to 5 I'm still having issues with having an SQL query in my constructor, and then accessing the results later on through functions.

Even when I just put regular text in there to be displayed I get nothing when calling it later on. Here is a snippet of the code:

Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3. class News
  4. {
  5.     private $private = 'Private';
  6.  
  7.     function News()
  8.     {
  9.  
  10.         include('connect.php');
  11.  
  12.         $query = "SELECT * FROM `news` WHERE `user` LIKE CONVERT( _utf8 'dot' USING latin1 ) ";
  13.         $result = mysql_query($query) or die(mysql_error());
  14.         $image =  mysql_result($result,0,"avatar"); 
  15.         $title =  mysql_result($result,0,"title"); 
  16.         $story =  mysql_result($result,0,"post");
  17.         $date =  mysql_result($result,0,"date"); 
  18.     }
  19.  
  20.  
  21.     function loadImage()
  22.     {
  23.  
  24.         echo $image;
  25.  
  26.  
  27.     }
  28.  
  29. ?>
I initialize the class like this:
Expand|Select|Wrap|Line Numbers
  1. //CSS link and header stuff above
  2. <body>
  3. <?php
  4. include('newsClass.php');
  5. $news = new News();
  6.  
  7. ?>
  8. <table width="95%" border="0" cellpadding="0" cellspacing="0">
  9.  
  10. //More HTML table goodness below
  11.  
and then call the function like this:
Expand|Select|Wrap|Line Numbers
  1.     <td width="10%"><img name="" src="users/<? $news->loadImage();?>" width="100" height="100" alt="" /></td>
I can only get the function to work if and only if I make the function look like this:
Expand|Select|Wrap|Line Numbers
  1.     function loadImage()
  2.     {
  3.  
  4.         $query = "SELECT * FROM `news` WHERE `user` LIKE CONVERT( _utf8 'dot' USING latin1 ) ";
  5.         $result = mysql_query($query) or die(mysql_error());
  6.         $image =  mysql_result($result,0,"avatar"); 
  7.         echo $image;
  8.  
  9.  
  10.     }
  11.  
Now i have about 4 other functions which need to make the same SQL connection, but the only difference being that the name changes for the appropriate SQL column. It seems horribly inefficient to me to repeatedly do the same thing over and over.
Any suggestions?
  #6  
Old June 21st, 2009, 08:20 AM
Markus's Avatar
Moderator
 
Join Date: Jun 2007
Location: York, England, with wolves.
Posts: 4,862
Provided Answers: 9

re: Visibility issues using classes


That's because you're not saving the variables into the object's scope. You just create them in a function - once that function is finished, the variables are gone.

You should be using the $this keyword:

Expand|Select|Wrap|Line Numbers
  1. class Test {
  2.  
  3.     // Create your properties here
  4.     private $private;
  5.     protected $protected;
  6.     public $public;
  7.  
  8.     public function Test() 
  9.    {
  10.         $this->private = "..."
  11.         $this->protected = "...";
  12.         // ... You get the idea.
  13.     }
  14.  
  15.     public function get_private()
  16.    {
  17.         return $this->private;
  18.     }  
  19. }
  20.  

Last edited by Atli; June 21st, 2009 at 02:32 PM. Reason: Fixed a typo in the code ;)
  #7  
Old June 26th, 2009, 10:05 PM
Member
 
Join Date: Dec 2007
Posts: 37

re: Visibility issues using classes


Thanks alot, your suggestion fixed it all. Now I'm using PHP like an OO language and I don't plan on ever looking back.
Reply


Similar Threads
Thread Thread Starter Forum Replies Last Post
override method without extending visibility Ben Voigt answers 13 November 13th, 2006 08:05 AM
Interop issues Varun Bansal answers 1 May 21st, 2006 12:35 PM
Viewstate issues after move to 2.0 from 1.1 Robert answers 10 November 25th, 2005 06:25 AM
"pointers" in /clr Peter Oliphant answers 17 November 19th, 2005 12:15 AM