Jump to content
php.lv forumi

Recommended Posts

Posted

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?

Posted

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;
}

Posted

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.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...