PDA

Pogčedajte punu verziju : Varijable - vidljivost unutar klase


bojan_bozovic
03. 02. 2006., 22:10
class mysql_link {

var $link_id;
var $queries=0;
var $sqldump;
function mysql_link_connect($hostname,$username,$password,$ database) {

$link_id=mysql_connect($hostname,$username,$passwo rd);

if (!$link_id) {
echo('Database server is down - symptom: invalid username, password or server URI');
echo(' <a href="mailto:'.ADMIN_EMAIL.'?subject=Database is down!">Click here to email to administrator that site is down</a>. Press the back button, please. Sorry for the inconvenience.');
die(); }

if (!mysql_select_db($database)) {
echo('Database server is down - symptom: site cannot select database specified');
echo(' <a href="mailto:'.ADMIN_EMAIL.'?subject=Database is down!">Click here to email to administrator that site is down</a>. Press the back button, please. Sorry for the inconvenience.');
die(); }

return $link_id;
}

function mysql_link_release() {
mysql_close($link_id);
}

function mysql_link_query($query_db) {
global $queries,$sqldump;
$result_db=mysql_query($query_db);

if (!$result_db) die("Problematic SQL query ::: <br /><br />".$query_db."<br /><br />Please <a href=\"mailto:".ADMIN_EMAIL."?subject=Bug in SQL at domain ".$_SERVER["SERVER_NAME"]."\">email to administrator</a> that there are technical difficulties");

$queries=$queries+1;
$sqldump=$sqldump."\n".$query_db;
return $result_db;
}
function dump_sql() {
global $sqldump;
return $sqldump;
}
function dump_query_number() {
global $queries;
return $queries;
}
}

$queries i $sqldump su globalno vidljive. nesto poput

function dump_sql() {
return $this->$sqldump;
}
Nece da radi (dobijam cannot reference empty resource ili sl). Kako $queries i $sqldump da budu vidljive samo unutar klase mysql_link. Primer mogu i echo $link_id->dump_query_number(); i echo $queries;

kalkulus
03. 02. 2006., 22:31
treba da pishe

function dump_sql() {
return $this->sqldump;
}

znaci bez $ posle ->

shto se tice vidljivosti ako koristish php4 nikako ih ne mozesh ograniciti na maticnu klasu, barem koliko ja znam

u php5 mozesh da definishesh promenljive kao private i to je to

ivanhoe
04. 02. 2006., 04:43
ako ces imati samo jednu instancu objekta mozes u php4 da simuliras privatne propertije preko static varijabli u funkciji :

class A {
function property_a($value= NULL) {
static $a;
if(isset($value))
$a = $value;
else
return $a;
}

function set_a($value) {
$this->property_a($value);
}

function get_a() {
return $this->property_a();
}
}

naravno ovo i dalje ne sprecava nekog da pozove direktno funkciju property_a()... to bi doduse moglo da se spreci proverom kako je pozvana funkcija pomocu debug_backtrace(), i da vratis null ako je pozvana direktno...

ali realno, ovo je previse gnjavaze za moj ukus :)