Visibility issues using classes 
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: - 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: -
-
<?php
-
-
class News
-
{
-
public $private = 'Private';
-
-
function News()
-
{
-
-
include('connect.php');
-
}
-
-
-
function loadImage()
-
{
-
$query = "SELECT * FROM `news` WHERE `user` LIKE CONVERT( _utf8 'dot' USING latin1 ) ";
-
$result = mysql_query($query) or die(mysql_error());
-
$image = mysql_result($result,0,"avatar");
-
echo $image;
-
-
-
}
-
-
function loadTitle()
-
{
-
$query = "SELECT * FROM `news` WHERE `user` LIKE CONVERT( _utf8 'dot' USING latin1 ) ";
-
$result = mysql_query($query) or die(mysql_error());
-
$title = mysql_result($result,0,"title");
-
echo $title;
-
}
-
-
function loadStory()
-
{
-
$query = "SELECT * FROM `news` WHERE `user` LIKE CONVERT( _utf8 'dot' USING latin1 ) ";
-
$result = mysql_query($query) or die(mysql_error());
-
$story = mysql_result($result,0,"post");
-
echo $story;
-
}
-
-
function loadComments()
-
{
-
-
}
-
-
function loadDate()
-
{
-
$query = "SELECT * FROM `news` WHERE `user` LIKE CONVERT( _utf8 'dot' USING latin1 ) ";
-
$result = mysql_query($query) or die(mysql_error());
-
$date = mysql_result($result,0,"date");
-
echo $date;
-
}
-
-
}
-
-
?>
-
-
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.: - <?php
-
-
class News
-
{
-
public $private = 'Private';
-
-
function News()
-
{
-
-
include('connect.php');
-
$query = "SELECT * FROM `news` WHERE `user` LIKE CONVERT( _utf8 'my_user' USING latin1 ) ";
-
$result = mysql_query($query) or die(mysql_error());
-
}
-
-
-
function loadImage()
-
{
-
-
$image = mysql_result($result,0,"avatar");
-
echo $image;
-
-
-
}
-
-
function loadTitle()
-
{
-
$title = mysql_result($result,0,"title");
-
echo $title;
-
}
-
-
function loadStory()
-
{
-
$story = mysql_result($result,0,"post");
-
echo $story;
-
}
-
-
function loadComments()
-
{
-
-
}
-
-
function loadDate()
-
{
-
$date = mysql_result($result,0,"date");
-
echo $date;
-
}
-
-
}
-
-
?>
Any assistance is much appreciated!
| 
June 15th, 2009, 08:43 AM
|  | 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
| 
June 15th, 2009, 12:54 PM
|  | 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.
| 
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.
| 
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: - <?php
-
-
class News
-
{
-
private $private = 'Private';
-
-
function News()
-
{
-
-
include('connect.php');
-
-
$query = "SELECT * FROM `news` WHERE `user` LIKE CONVERT( _utf8 'dot' USING latin1 ) ";
-
$result = mysql_query($query) or die(mysql_error());
-
$image = mysql_result($result,0,"avatar");
-
$title = mysql_result($result,0,"title");
-
$story = mysql_result($result,0,"post");
-
$date = mysql_result($result,0,"date");
-
}
-
-
-
function loadImage()
-
{
-
-
echo $image;
-
-
-
}
-
-
?>
I initialize the class like this: -
//CSS link and header stuff above
-
<body>
-
<?php
-
include('newsClass.php');
-
$news = new News();
-
-
?>
-
<table width="95%" border="0" cellpadding="0" cellspacing="0">
-
-
//More HTML table goodness below
-
and then call the function like this: - <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: - function loadImage()
-
{
-
-
$query = "SELECT * FROM `news` WHERE `user` LIKE CONVERT( _utf8 'dot' USING latin1 ) ";
-
$result = mysql_query($query) or die(mysql_error());
-
$image = mysql_result($result,0,"avatar");
-
echo $image;
-
-
-
}
-
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?
| 
June 21st, 2009, 08:20 AM
|  | 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: -
class Test {
-
-
// Create your properties here
-
private $private;
-
protected $protected;
-
public $public;
-
-
public function Test()
-
{
-
$this->private = "..."
-
$this->protected = "...";
-
// ... You get the idea.
-
}
-
-
public function get_private()
-
{
-
return $this->private;
-
}
-
}
-
Last edited by Atli; June 21st, 2009 at 02:32 PM.
Reason: Fixed a typo in the code ;)
| 
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.
|  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 225,689 network members.
|