help with class

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
forgun
Forum Commoner
Posts: 61
Joined: Wed Jan 29, 2003 6:05 am
Contact:

help with class

Post by forgun »

i have a prob about a class that conect to db
this is the code of the class

Code: Select all

<?
class dataset &#123;
        var $sets = array();
        function pros() &#123;
                include("dbs.php");
                $lk = mysql_connect($serv,$user,$pass) or die(mysql_error());
                mysql_select_db($dbname&#1111;0],$lk);
                $qur = "select * from pollqa order by id desc limit 0,1";
                $res = mysql_query($qur) or die ( "Error display \n" .mysql_error());
                while ($row = mysql_fetch_assoc($res)) &#123;
                $this->sets&#1111;0] = $row&#1111;'qs'];
                $this->sets&#1111;1] = $row&#1111;'ans1'];
                $this->sets&#1111;2] = $row&#1111;'ans2'];
                $this->sets&#1111;3] = $row&#1111;'ans3'];
                $this->sets&#1111;4] = $row&#1111;'ans4'];
                $this->sets&#1111;5] = $row&#1111;'ans5'];
                $this->sets&#1111;6] = $row&#1111;'ans6'];
                &#125;
                return $this->sets;
        &#125;
&#125;
?>
the code of the display page

Code: Select all

<?
session_start();
include_once("includes/pross.inc");
$infos = new dataset();
$infos->pros();

$ip = $_SERVER&#1111;'REMOTE_ADDR'];
session_register($sid ,$vote);
$_SESSION&#1111;'SID'] = session_id();
if (is_array($infos)) &#123;
        foreach ($infos as $pres => $i) &#123;
?>
<html>
<body>
<form action="polrs.php" method="POST" id="poll">
<p>
<H3><b><?= $infos&#1111;0] ?> </b></h3></p><br/><hr><br/>
<?
if ($i != 0) &#123;
        echo "<INPUT type="checkbox" value=" $i "> $pres <br/>";
&#125;
&#125;
&#125;

?>
<hr><br/>
<input type="hidden" value="<?=$ip?>">
<input type="hidden" value="<?= $_SESSION&#1111;'SID']?>"
<INPUT type="submit" value="Enter vote">
</body>
</html>
the prob is that not data been recive(=coming) to the page
User avatar
protokol
Forum Contributor
Posts: 353
Joined: Fri Jun 21, 2002 7:00 pm
Location: Cleveland, OH
Contact:

Post by protokol »

Notice that pros() returns the array. You do this:

Code: Select all

$infos = new dataset();
$infos->pros();
What you need to do is this:

Code: Select all

$my_dataset = new dataset();
$infos = $my_dataset->pros();
Then your $infos will contain the array that you intended it to have.
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

If you're using PHP4..

var $sets = array();

..won't work: you can't initialise a class var with a function. You'd have to build a constructor ie a function with the same name. Basically, you'd write:

var $sets;

.. and change the name of pros() to dataset().

Mind you, since the class doesn't do anything else except initialise the $sets array, all you need is a simple function rather than a whole class.

There's been a few other posts about classes recently which might be worth looking at.
User avatar
protokol
Forum Contributor
Posts: 353
Joined: Fri Jun 21, 2002 7:00 pm
Location: Cleveland, OH
Contact:

Post by protokol »

McGruff ... you are wrong. You can legally define the value of the member variable when you initialize it.

Code: Select all

class Blah &#123;
   var $sets = array();
&#125;
This is 100% valid.

You can ALSO do:

Code: Select all

class Blah &#123;
   var $sets;

   function Blah() &#123;
      $this->sets = array();
   &#125;
&#125;
Both do the same thing and are completely legal. It is a matter of preference.
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

McGruff ... you are wrong. You can legally define the value of the member variable when you initialize it.
Not according to the manual:
Note: In PHP 4, only constant initializers for var variables are allowed.
User avatar
protokol
Forum Contributor
Posts: 353
Joined: Fri Jun 21, 2002 7:00 pm
Location: Cleveland, OH
Contact:

Post by protokol »

I DID specify a constant value. Therefore it is legal.
Last edited by protokol on Tue Mar 04, 2003 10:41 am, edited 1 time in total.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

Code: Select all

<?php
class CTest
{
	var $testVar = array();
	function CTest()
	{
		print_r($this->testVar);
	}
}

$t = new CTest();
?>
good to know, thx protokol
jason
Site Admin
Posts: 1767
Joined: Thu Apr 18, 2002 3:14 pm
Location: Montreal, CA
Contact:

Post by jason »

From the manual:

Note: In PHP 4, only constant initializers for var variables are allowed.

This means

Code: Select all

var $var = 'bob'.'ert';
is wrong.

However, both

Code: Select all

var $var = 'bob';
var $array = array();
are allowed.

This is because array() is NOT a function, but a language construct.

Code: Select all

var $str = nl2br($content);
This would not work either. nl2br() IS a function.

array() returns a constant value, an empty array.

However,

Code: Select all

var $array = array("jason", "lotito");
would not work.
User avatar
BDKR
DevNet Resident
Posts: 1207
Joined: Sat Jun 08, 2002 1:24 pm
Location: Florida
Contact:

Post by BDKR »

Hey,

I'm sorry you guys if I sound like an old woman, but isn't there something wrong with this picture. Nobody is doing forgun any good by helping him get this hunk of code working. The reason being that there are multiple areas of responsibility slammed into one method in a class! This is not what good design, OO or otherwise, is about. Writing classes in this manner does not create any useful or reusable abstraction and makes this hunk of code in particular too critical.

A breakdown (I could've sworn I said this before!):

* All of the db connection and related functions should be in their own class.
* All of the query and row fetching ops should be in their own class.
* The var $sets should be it's own class.

Like I said, I may sound like an evil Nazi school master about this time, but trust me on this one. Pushing a bad position, or as in this case, a bad design, is ultimately going to bite you in the tail.

Here is a list of advantages to a different design.

* If you need to use a different db, it should be no problem as a well written abstraction layer won't force a code rewrite. This design will.
* It's reusable!
* It's not critical as each component is easily changed without risk of messing up
another.

I would say the problem is one of couping, but that can't be if there is only one class. This is more or less a GodClass. Parameter coupling on the other hand wouldn't be too much of an issue, but like I said before, you first need more than one class or module.

Anyways, I'd better shut off now as I'm sure to get flamed for being a tosser.

Cheers,
BDKR
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

Good points BD. A sticky about when to use and not use classes might be good. Because they're there, and they look complicated, beginners feel they have to use them to be a real php programmer (not casting any asperions your way forgun) but I often wonder how many classes out there on the web are actually justified?
jason
Site Admin
Posts: 1767
Joined: Thu Apr 18, 2002 3:14 pm
Location: Montreal, CA
Contact:

Post by jason »

Good tutorial on how to create classes.

http://www.phppatterns.com/index.php/ar ... ew/31/1/1/
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

Thanks for the link.
Post Reply