sangoku Posted February 15, 2010 Share Posted February 15, 2010 Hy i have a big problem i have a class class a(){ $a; $b; . . function _construct() { check some value; self::do-some(); } function do-some(){ //this f. does retrieve data from DB and needs to return it as the class that is just instantiated how to do it??? it forms a class but as it forms it it atempts to make a new class which creates a endless loop } } Quote Link to comment https://forums.phpfreaks.com/topic/192154-oop-dont-wana-do-what-i-want/ Share on other sites More sharing options...
wildteen88 Posted February 15, 2010 Share Posted February 15, 2010 Hy i have a big problem i have a class And... the problem is what? What are you trying to do? What errors are you getting etc. Need more information about your problem Quote Link to comment https://forums.phpfreaks.com/topic/192154-oop-dont-wana-do-what-i-want/#findComment-1012666 Share on other sites More sharing options...
sangoku Posted February 15, 2010 Author Share Posted February 15, 2010 no error an ENDLESS loop i cant find a way to limit the call from the construct intern content, it calss it selff over and over again. Any way i just decidet that i will call the function outside of the constructor it is not pretty but,,, im out of options. i I could see if object exists but then if i ever needet to call more of thosue classes i would be ina giant O.o Ps. the problem is explained in the code coment... Quote Link to comment https://forums.phpfreaks.com/topic/192154-oop-dont-wana-do-what-i-want/#findComment-1012682 Share on other sites More sharing options...
PHP Monkeh Posted February 15, 2010 Share Posted February 15, 2010 I'm trying to figure out whether this is a joke or not. Anyway, you'll need to show us some actual code if you want us to help. There's no use mentioning that something is in an 'endless loop', when the code you've shown doesn't actually contain a loop. And please spell-check your posts before posting as they're hard to understand. Quote Link to comment https://forums.phpfreaks.com/topic/192154-oop-dont-wana-do-what-i-want/#findComment-1012718 Share on other sites More sharing options...
Splash Posted February 15, 2010 Share Posted February 15, 2010 Not sure if it's the cause of your problem but your constructor should start with 2 underscores. Quote Link to comment https://forums.phpfreaks.com/topic/192154-oop-dont-wana-do-what-i-want/#findComment-1012721 Share on other sites More sharing options...
sangoku Posted February 15, 2010 Author Share Posted February 15, 2010 i corrected it XD but not how i wanted i will post the files that are included ok i have a class: class bruteAttackLog extends DatabaseObject { protected $kljuc = 'IP_adresa'; protected static $table_name = 'brute_attack_log'; protected static $db_fields = array('IP_adresa','broj_napada','zakljucana_do','broj_zakljucavanja','trajno_zatvorena','MAC_adresa'); public $IP_adresa; public $broj_napada; public $zakljucana_do; public $broj_zakljucavanja; public $trajno_zatvorena; public $MAC_adresa; /** * stvara, updejtuje banuje pri instanciranju * * @param curent $ip * */ function newLog() { //ukoliko imamo ip adresu zabiljezenu unutar baze onda $log = self::find_by_IP(); if (!$log){//provjeravamo dali ima data ip zabilježena $this->IP_adresa = funkcijeOsnovne::uzmiIP(); $this->broj_napada = 1; $this->create();//ukoliko ne onda pravimo novi zapis } else {// kada postoji objekat radimosliedeće operacije //provjeravamo dali je prekoracen max br napada if($log->broj_napada <= MAX_NAPADA) { $log->zakljucana_do = date('c',time()-1); // postavljamo zakljucavanje na proslo vreme tako da imamo vreme zadnjeg ristupa $log->broj_napada += 1; $log->update(); } else { //postavljamo zaključavanje na 3 sata $log->zakljucana_do = date('c',(time() + (3* 60 * 60))); $log->broj_napada = 0; $log->broj_zakljucavanja += 1; $log->update(); } } } function __destruct() { } /** * Vraca vrednost false ukoliko nema unosa, permanent ukoliko je permanentan ban * i vrijeme preostalog bana u seundama * * @param IP $ip * @return false, permanent, time remaining */ public static function daliJeBanovana($ip){//provjeravamo dali je ip banovan $ip = self::find_by_IP($ip);//nalazimo ip u bazi podazi podataka if (!$ip){ return false;//ukolilkop ne postoji ip u bazi vracamo false } if ($ip->trajno_zatvorena){ return 'permanent'; //ukoliko je trajno zatvorena vracamo 'permanent' } if ( isset($ip->zakljucana_do)){ $ban = mktime($ip->zakljucana_do); if ($ban >= time() ){ $ban = $ban - time(); return $ban; } } } private static function find_by_IP() { $res = self::find_by_id(ip2long(funkcijeOsnovne::uzmiIP()),'IP_adresa'); if ($res) $res ->IP_adresa = long2ip($res->IP_adresa) ; return $res; } /** * postavlja trajno Zatvaranje na true * * @param IP $ip * */ private function zakljucavac($ip){ $ip = self::find_by_IP($ip); $ip->trajno_zatvorena = true; $ip->update(); } public function update(){ $this->IP_adresa = ip2long($this->IP_adresa); parent::update(); } public function create(){ $this->IP_adresa = ip2long($this->IP_adresa); parent::create(); } public function delete(){ $this->IP_adresa = ip2long($this->IP_adresa); return parent::delete(); } } and the extendet class class DatabaseObject { /** * nađi sve u datoj tabeli * */ public static function find_all() { return self::find_by_sql("SELECT * FROM " .static::$table_name); } /** * nađi u tabeli id vrednost prema koloni ID_is * * @param int $id * @param text $kljuc * @return mixed */ public static function find_by_id($id=0, $kljuc) { $result_array = static::find_by_sql("SELECT * FROM ".static::$table_name." WHERE {$kljuc}={$id} LIMIT 1"); return !empty($result_array) ? array_shift($result_array) : false; } /** * Izvršavanje sql upita sa instanciranjem * * @param mixed $sql */ public static function find_by_sql($sql="") { global $DB; $result_set = $DB->query($sql); $object_array = array(); //OVO INSTANCIRA VIŠE OBJEKATA ODJEDNOM!!!!!! //OVO INSTANCIRA VIŠE OBJEKATA ODJEDNOM!!!!!! while ($row = $DB->fetch_array($result_set)) { $object_array[] = static::instantiate($row); } //OVO INSTANCIRA VIŠE OBJEKATA ODJEDNOM!!!!!! //OVO INSTANCIRA VIŠE OBJEKATA ODJEDNOM!!!!!! return $object_array; } /** * Nađi koliko elemenata je u tabeli * @return int $read */ public static function count_all(){ global $DB; $sql = "SELECT COUNT( * ) FROM ".static::$table_name; $rezult_raw = $DB->query($sql); $red = $DB->fetch_array($rezult_raw); return array_shift($red); } /** * iscitava atribute iz klase i vraca ih kao array * * @return array $atributi */ protected function attributes() { $attributes = array(); foreach (static::$db_fields as $field){ if (property_exists($this,$field )) { $attributes[$field] = $this->$field; } } return $attributes; } /** * uvraca ociscene atribute od nepotrebnih vrednosti * * @return array $cisti_atributi */ protected function sanitized_attributes() { global $DB; $clean_atttributes = array(); foreach ($this->attributes() as $key => $value ){ $clean_atttributes[$key] = $DB->sql_prep($value); } return $clean_atttributes; } /** * update izaziva update * * @param autoincrement $id * @param update */ public function create(){ global $DB; $atrributes = $this->sanitized_attributes(); $sql = "INSERT INTO ".static::$table_name." (". join(", ", array_keys($atrributes)) .") VALUES ('"; $sql .= join("', '",array_values($atrributes) ); $sql .= "')"; if ($DB->query($sql)) { $key = $this->kljuc; $this->$key =$DB->insert_id(); // pozivamo $this->$this->kljuc da bi vrednost kljuc bila pozvana kao atribut $this return TRUE; } else { return FALSE; } } public function update(){ global $DB; $atttributes = $this->sanitized_attributes(); $atttribute_pairs = array(); //nalazimo trenutne vrednosti objekta i pohranjujemo ga u array foreach ($atttributes as $key =>$value ){ $atttribute_pairs[] = "{$key} = '{$value}'"; } $sql = "UPDATE ".static::$table_name." SET "; //dati array koristimo kao upit $sql .=join(", ",$atttribute_pairs)." "; $kljuc = $this->kljuc; $sql .="WHERE {$DB->sql_prep($this->kljuc)} = ".$DB->sql_prep($this->$kljuc); //id predstavlja ime referencirane autoinkrementirane kolone $DB->query($sql); return($DB->affected_rows() == 1) ? TRUE : FALSE; } public function delete(){ global $DB; $sql = "DELETE FROM ".static::$table_name. " WHERE ".$DB->sql_prep($this->kljuc)." = "; $sql .= $DB->sql_prep($this->$this->kljuc); $DB->query($sql); return($DB->affected_rows() == 1) ? TRUE : FALSE; } /** * instancira objekat po zavrsavanju upita * * @param mixed $record * @return object $object */ private static function instantiate($record) { $class_name = get_called_class(); $object = new $class_name; foreach($record as $attribute=>$value){ if($object->has_attribute($attribute)) { $object->$attribute = $value; } } return $object; } /** * provjera dali postoji dati atribut * * @param mixed $attribute * @return bool */ private function has_attribute($attribute) { $object_vars = $this->attributes($this); return array_key_exists($attribute, $object_vars); } } and the class this class extends <?php /** * @author Siniša Čulić * @version 1.0 * * @created 10-02-2010 01:31:22 */ //provijera pristupa i logovanje napada if (!defined('ACESS')){// ukoliko pristup nije opravdan radi: if (!defined('SITE_ROOT')) { $sada = $_SERVER['PHP_SELF']; //saznajemo lokaciju samoga fajla $root = explode('/',$sada, 3);// izdvajamo samo lokacju osnovnog foldera define ('SITE_ROOT',$_SERVER['DOCUMENT_ROOT'].$root[1]); // i date informacije definisemo } include_once(SITE_ROOT.'/protected/internaSigurnost.php'); }// kraj provjere sigurnosti class MySQLDatabase{ //Privatni atributi private $connection; private $magic_quotes_active; private $real_escape_string_exists; //---------------javni atributi:-------------*/ public $zadni_upit; //---------------kraj javni atributi:-------------*/ //konstruktor za datu klasu function __construct(){ $this->open_conection();//otvaramo konekciju pri inicijalizaciji $this->magic_quotes_active = get_magic_quotes_gpc(); $this->real_escape_string_exists = function_exists( "mysql_real_escape_string" ); // i.e. PHP >= v4.3.0 } //javna metoda otvaranje konekcije public function open_conection(){ $this->connection = mysql_connect(DB_SERVER,DB_USER,DB_PASS); if (!$this->connection) { //die("Database connection failed: " . mysql_error()); log::loguj('SQL',$this->zadni_upit); }else { $db_select = mysql_select_db(DB_NAME, $this->connection); if (!$db_select) { log::loguj('SQL',$this->zadni_upit); } } } //javna metoda //zatvori konekciju public function close_connection(){ if (isset($this->connection)) { mysql_close($this->connection); unset($this->connection); } } //Javna metoda //Izvršava upit $sql i provijeranva dali je izvršen upit public function query($sql) { $this->zadni_upit= $sql;//pohranuje vrijednost u zadnji upit $result = mysql_query($sql, $this->connection); $this->confirm_query($result); return $result; } //Priprema pohranjivanja u mysql bazu public function sql_prep( $value ) { if( $this->real_escape_string_exists ) { // PHP v4.3.0 ili stariji // undo bilo koje magic quote effects tako da mysql_real_escape_string može da radi if( $this->magic_quotes_active ) { $value = stripslashes( $value ); } $value = mysql_real_escape_string( $value ); } else { // Prije PHP v4.3.0 // ako magic quotes nisu već upaljene onda dodaj / manuelno if( !$this->magic_quotes_active ) { $value = addslashes( $value ); } // Ako su magic quotes aktivne, onda / već su unutra } return $value; } //Intermodulacija date funkcije public function fetch_array ($rez){ return mysql_fetch_array($rez); } //Intermodulacija date funkcije public function num_rows($rez){ return mysql_num_rows($rez); } //Uzmi zadnji id ubacen preko trenutne DB konekcije //pravi datu funkciju modularnom public function insert_id(){ return mysql_insert_id($this->connection); } //Normalizacija funkcije mysql_affected_rows tako da je modularno kompatibilna function affected_rows(){ return mysql_affected_rows($this->connection); } //Privatni atribut //Provijera upita koji se poziva, pozivaju samo funkcije unutar klase DB private function confirm_query($result){ if (!$result) { $output = "Database query failed: ". mysql_error()."<br />"; $output.="Last Query: ". $this->zadni_upit; log::loguj('SQL',$output); } } } ?> And the first class mentioned here bruteAttackLog needs to populates him self by calling it like $a= new bruteAttackLog(); and the function newLog should be called when the object is called. but when i call it i get a empty array The $a gets an empty reply if an update or save ocures. can somone help me out here????? As told earlier, it is on serbian i sugest to use the google translator so ou get more meaning of it. Quote Link to comment https://forums.phpfreaks.com/topic/192154-oop-dont-wana-do-what-i-want/#findComment-1012764 Share on other sites More sharing options...
sangoku Posted February 15, 2010 Author Share Posted February 15, 2010 ok solved it but still have some quirks. Quote Link to comment https://forums.phpfreaks.com/topic/192154-oop-dont-wana-do-what-i-want/#findComment-1012860 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.