fanatos Posted May 22, 2009 Report Share Posted May 22, 2009 class Photo{ ...... public function delete() { global $mysqli; $sql = "DELETE FROM " .static::$table_name; $sql .= " WHERE album_id =". $mysqli->real_escape_string($this->album_id); $sql .= " LIMIT 1"; $mysqli->query($sql); return ($mysqli->affected_rows == 1) ? true : false; } } $album->delete() REZUKLTAATS: Fatal error: Call to undefined method stdClass::delete() Varbuut kaads var pateikt, kas sheit ir par vainu? Quote Link to comment Share on other sites More sharing options...
bubu Posted May 22, 2009 Report Share Posted May 22, 2009 Kas ir $album? Quote Link to comment Share on other sites More sharing options...
fanatos Posted May 22, 2009 Author Report Share Posted May 22, 2009 Kas ir $album? // atgiezz Album objektu $album = Album::find_by_id(1); // atribuuts straadaa echo $album->title; // metode nee $album->delete(); Quote Link to comment Share on other sites More sharing options...
fanatos Posted May 22, 2009 Author Report Share Posted May 22, 2009 labi, es laikam zinu kur ir probleema Quote Link to comment Share on other sites More sharing options...
PHsG Posted May 22, 2009 Report Share Posted May 22, 2009 $album = new Photo(); Quote Link to comment Share on other sites More sharing options...
bubu Posted May 22, 2009 Report Share Posted May 22, 2009 labi, es laikam zinu kur ir probleema Tad pastāsti arī citiem. Quote Link to comment Share on other sites More sharing options...
fanatos Posted May 22, 2009 Author Report Share Posted May 22, 2009 metode, kas saveido abjektus bija noimplementaata nepareizi. tas kas izsauca: public static function find_by_id($id=0) { global $mysqli; $object_array = static::find_by_sql("SELECT * FROM ".static::$table_name." WHERE album_id=".$mysqli->real_escape_string($id)." LIMIT 1"); return !empty($object_array) ? array_shift($object_array) : false; } pirms: public static function find_by_sql($sql="") { global $mysqli; $object_array = array(); $result = $mysqli->query($sql); $mysqli->confirm_query($result); while ($obj = $result->fetch_object()) { $object_array[] = $obj; } $result->close(); return $object_array; } peec: public static function find_by_sql($sql="") { global $mysqli; $members = array(); // get class name at runtime $class_name = get_called_class(); $class_attributes = static::attributes(); $result = $mysqli->query($sql); $mysqli->confirm_query($result); while ($obj = $result->fetch_object()) { // set new instances $member = new $class_name; foreach (array_keys($class_attributes) as $key) { $member->$key = $obj->$key; } $members[] = $member; } $result->close(); return $members; } bet tagad esmu nonaacis pie citas probleemas: cik es tagad palasiiju, tad statiskaas metodes nevar izmatot vaardu $this liidz ar to es netieku klaat dazziem klases atribuutiem.. ir kaads variants, kaa es vinnis vareetu tomeer ielasiit? protected static function attributes() { $attributes = array(); foreach( static::table_fields() as $field ) { $class_name = get_called_class(); if(property_exists($class_name, $field)) { $attributes[$field] = $field; [b]//$this->$field[/b] } } return $attributes; } Quote Link to comment Share on other sites More sharing options...
bubu Posted May 22, 2009 Report Share Posted May 22, 2009 Jā, ir. Nelietot static. Static jau lieto tieši tāpēc, lai nebūtu $this - t.i. funkcija izpildītos globālā, ne objekta kontekstā. Vienīgais, kas kopējs static un ne-static funkcijām, ir tās neimspeiss - klases vārds. Quote Link to comment 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.