Klassenvariablen
PHP hat ein scheußĺiches Feature: Dynamik.
class Foobar {
/**
* @constructor
*/
function Foobar($params) {
foreach ($params as $key=>$val) {
$this->$key= $val;
}
}
}
hiermit erzeugt man einfach mal beliebige Klassenvariablen. Man kann das auch gleich noch besonders toll machen und "magische" getter und setter benutzen:
public function __set($var, $val){
$this->$var = $val;
}
public function __get($var){
if(isset($this->$var)){
return $this->$var;
} else {
throw new Exception("Property ‘$var’ does not exist");
}
}
Echt toll. Somit weiß man _nie_ welche Variablen ein Objekt zur Zeit hat. Tolle Sache.
Code Examples, Part 1
Gerade hab ich folgendes Code-Beispiel gefunden (nein, nicht auf der Arbeit, sondern "im Internet")
function is_negative($number) {
return substr((string)$number, 0, 1) === '-';
}
Ich lass das einfach mal so stehen ...
Powerpoint mit PHP
Wieder was interessantes aus der Gegend von PHP. Office 2007 baut ja inzwischen XML-basierte Dateien, d.h. das Erstellen von Office-Dokumenten ist erstaunlich einfach geworden. Nun gibt es neben der Excel-Klasse auch noch Powerpoint per PHP.
Ein Quellcodebeispiel gibt es hier: http://blog.maartenballiauw.be/post/2009/04/27/PHPPowerPoint-010-(CTP1)-released!.aspx
Das Zipfile das es zum Download gibt, enthält auch vier Testfälle. In meinem Fall musste ich noch den XMLWriter mit in PHP einkompilieren und danach noch ein paar Dateien und Verzeichnisse umbennen, da Linux bekanntlich sehr auf Groß- und Kleinschreibung achtet (PHPPowerpoint.php != PHPPowerPoint.php). Anyway, hier ein Beispielcode aus dem Zip und danach die generierte Powerpoint-Datei:
<?php
/**
* PHPPowerPoint
*
* Copyright (c) 2009 - 2010 PHPPowerPoint
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* @category PHPPowerPoint
* @package PHPPowerPoint
* @copyright Copyright (c) 2009 - 2010 PHPPowerPoint (http://www.codeplex.com/PHPPowerPoint)
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
* @version 0.1.0, 2009-04-27
*/
/** Error reporting */
error_reporting(E_ALL);
/** Include path **/
set_include_path(get_include_path() . PATH_SEPARATOR . '../Classes/');
/** PHPPowerPoint */
include 'PHPPowerPoint.php';
/** PHPPowerPoint_IOFactory */
include 'PHPPowerPoint/IOFactory.php';
// Create new PHPPowerPoint object
echo date('H:i:s') . " Create new PHPPowerPoint object\n";
$objPHPPowerPoint = new PHPPowerPoint();
// Set properties
echo date('H:i:s') . " Set properties\n";
$objPHPPowerPoint->getProperties()->setCreator("Maarten Balliauw");
$objPHPPowerPoint->getProperties()->setLastModifiedBy("Maarten Balliauw");
$objPHPPowerPoint->getProperties()->setTitle("Office 2007 PPTX Test Document");
$objPHPPowerPoint->getProperties()->setSubject("Office 2007 PPTX Test Document");
$objPHPPowerPoint->getProperties()->setDescription("Test document for Office 2007 PPTX, generated using PHP classes.");
$objPHPPowerPoint->getProperties()->setKeywords("office 2007 openxml php");
$objPHPPowerPoint->getProperties()->setCategory("Test result file");
// Remove first slide
echo date('H:i:s') . " Remove first slide\n";
$objPHPPowerPoint->removeSlideByIndex(0);
// Create templated slide
echo date('H:i:s') . " Create templated slide\n";
$currentSlide = createTemplatedSlide($objPHPPowerPoint); // local function
// Create a shape (text)
echo date('H:i:s') . " Create a shape (rich text)\n";
$shape = $currentSlide->createRichTextShape();
$shape->setHeight(200);
$shape->setWidth(600);
$shape->setOffsetX(10);
$shape->setOffsetY(400);
$shape->getAlignment()->setHorizontal( PHPPowerPoint_Style_Alignment::HORIZONTAL_LEFT );
$textRun = $shape->createTextRun('Introduction to');
$textRun->getFont()->setBold(true);
$textRun->getFont()->setSize(28);
$textRun->getFont()->setColor( new PHPPowerPoint_Style_Color( 'FFFFFFFF' ) );
$shape->createBreak();
$textRun = $shape->createTextRun('PHPPowerPoint');
$textRun->getFont()->setBold(true);
$textRun->getFont()->setSize(60);
$textRun->getFont()->setColor( new PHPPowerPoint_Style_Color( 'FFFFFFFF' ) );
// Create templated slide
echo date('H:i:s') . " Create templated slide\n";
$currentSlide = createTemplatedSlide($objPHPPowerPoint); // local function
// Create a shape (text)
echo date('H:i:s') . " Create a shape (rich text)\n";
$shape = $currentSlide->createRichTextShape();
$shape->setHeight(100);
$shape->setWidth(930);
$shape->setOffsetX(10);
$shape->setOffsetY(10);
$shape->getAlignment()->setHorizontal( PHPPowerPoint_Style_Alignment::HORIZONTAL_LEFT );
$textRun = $shape->createTextRun('What is PHPPowerPoint?');
$textRun->getFont()->setBold(true);
$textRun->getFont()->setSize(48);
$textRun->getFont()->setColor( new PHPPowerPoint_Style_Color( 'FFFFFFFF' ) );
// Create a shape (text)
echo date('H:i:s') . " Create a shape (rich text)\n";
$shape = $currentSlide->createRichTextShape();
$shape->setHeight(600);
$shape->setWidth(930);
$shape->setOffsetX(10);
$shape->setOffsetY(100);
$shape->getAlignment()->setHorizontal( PHPPowerPoint_Style_Alignment::HORIZONTAL_LEFT );
$textRun = $shape->createTextRun('- A class library');
$textRun->getFont()->setSize(36);
$textRun->getFont()->setColor( new PHPPowerPoint_Style_Color( 'FFFFFFFF' ) );
$shape->createBreak();
$textRun = $shape->createTextRun('- Written in PHP');
$textRun->getFont()->setSize(36);
$textRun->getFont()->setColor( new PHPPowerPoint_Style_Color( 'FFFFFFFF' ) );
$shape->createBreak();
$textRun = $shape->createTextRun('- Representing a presentation');
$textRun->getFont()->setSize(36);
$textRun->getFont()->setColor( new PHPPowerPoint_Style_Color( 'FFFFFFFF' ) );
$shape->createBreak();
$textRun = $shape->createTextRun('- Supports writing to different file formats');
$textRun->getFont()->setSize(36);
$textRun->getFont()->setColor( new PHPPowerPoint_Style_Color( 'FFFFFFFF' ) );
// Create templated slide
echo date('H:i:s') . " Create templated slide\n";
$currentSlide = createTemplatedSlide($objPHPPowerPoint); // local function
// Create a shape (text)
echo date('H:i:s') . " Create a shape (rich text)\n";
$shape = $currentSlide->createRichTextShape();
$shape->setHeight(100);
$shape->setWidth(930);
$shape->setOffsetX(10);
$shape->setOffsetY(10);
$shape->getAlignment()->setHorizontal( PHPPowerPoint_Style_Alignment::HORIZONTAL_LEFT );
$textRun = $shape->createTextRun('What\'s the point?');
$textRun->getFont()->setBold(true);
$textRun->getFont()->setSize(48);
$textRun->getFont()->setColor( new PHPPowerPoint_Style_Color( 'FFFFFFFF' ) );
// Create a shape (text)
echo date('H:i:s') . " Create a shape (rich text)\n";
$shape = $currentSlide->createRichTextShape();
$shape->setHeight(600);
$shape->setWidth(930);
$shape->setOffsetX(10);
$shape->setOffsetY(100);
$shape->getAlignment()->setHorizontal( PHPPowerPoint_Style_Alignment::HORIZONTAL_LEFT );
$textRun = $shape->createTextRun('- Generate slide decks');
$textRun->getFont()->setSize(36);
$textRun->getFont()->setColor( new PHPPowerPoint_Style_Color( 'FFFFFFFF' ) );
$shape->createBreak();
$textRun = $shape->createTextRun(' - Represent business data');
$textRun->getFont()->setSize(28);
$textRun->getFont()->setColor( new PHPPowerPoint_Style_Color( 'FFFFFFFF' ) );
$shape->createBreak();
$textRun = $shape->createTextRun(' - Show a family slide show');
$textRun->getFont()->setSize(28);
$textRun->getFont()->setColor( new PHPPowerPoint_Style_Color( 'FFFFFFFF' ) );
$shape->createBreak();
$textRun = $shape->createTextRun(' - ...');
$textRun->getFont()->setSize(28);
$textRun->getFont()->setColor( new PHPPowerPoint_Style_Color( 'FFFFFFFF' ) );
$shape->createBreak();
$textRun = $shape->createTextRun('- Export these to different formats');
$textRun->getFont()->setSize(36);
$textRun->getFont()->setColor( new PHPPowerPoint_Style_Color( 'FFFFFFFF' ) );
$shape->createBreak();
$textRun = $shape->createTextRun(' - PowerPoint 2007');
$textRun->getFont()->setSize(28);
$textRun->getFont()->setColor( new PHPPowerPoint_Style_Color( 'FFFFFFFF' ) );
$shape->createBreak();
$textRun = $shape->createTextRun(' - Serialized');
$textRun->getFont()->setSize(28);
$textRun->getFont()->setColor( new PHPPowerPoint_Style_Color( 'FFFFFFFF' ) );
$shape->createBreak();
$textRun = $shape->createTextRun(' - ... (more to come) ...');
$textRun->getFont()->setSize(28);
$textRun->getFont()->setColor( new PHPPowerPoint_Style_Color( 'FFFFFFFF' ) );
// Create templated slide
echo date('H:i:s') . " Create templated slide\n";
$currentSlide = createTemplatedSlide($objPHPPowerPoint); // local function
// Create a shape (text)
echo date('H:i:s') . " Create a shape (rich text)\n";
$shape = $currentSlide->createRichTextShape();
$shape->setHeight(100);
$shape->setWidth(930);
$shape->setOffsetX(10);
$shape->setOffsetY(10);
$shape->getAlignment()->setHorizontal( PHPPowerPoint_Style_Alignment::HORIZONTAL_LEFT );
$textRun = $shape->createTextRun('Need more info?');
$textRun->getFont()->setBold(true);
$textRun->getFont()->setSize(48);
$textRun->getFont()->setColor( new PHPPowerPoint_Style_Color( 'FFFFFFFF' ) );
// Create a shape (text)
echo date('H:i:s') . " Create a shape (rich text)\n";
$shape = $currentSlide->createRichTextShape();
$shape->setHeight(600);
$shape->setWidth(930);
$shape->setOffsetX(10);
$shape->setOffsetY(100);
$shape->getAlignment()->setHorizontal( PHPPowerPoint_Style_Alignment::HORIZONTAL_LEFT );
$textRun = $shape->createTextRun('Check the project site on CodePlex:');
$textRun->getFont()->setSize(36);
$textRun->getFont()->setColor( new PHPPowerPoint_Style_Color( 'FFFFFFFF' ) );
$shape->createBreak();
$textRun = $shape->createTextRun(' http://phppowerpoint.codeplex.com');
$textRun->getFont()->setSize(36);
$textRun->getFont()->setColor( new PHPPowerPoint_Style_Color( 'FFFFFFFF' ) );
// Create templated slide
echo date('H:i:s') . " Create templated slide\n";
$currentSlide = createTemplatedSlide($objPHPPowerPoint); // local function
// Save PowerPoint 2007 file
echo date('H:i:s') . " Write to PowerPoint2007 format\n";
$objWriter = PHPPowerPoint_IOFactory::createWriter($objPHPPowerPoint, 'PowerPoint2007');
$objWriter->save(str_replace('.php', '.pptx', __FILE__));
// Echo memory peak usage
echo date('H:i:s') . " Peak memory usage: " . (memory_get_peak_usage(true) / 1024 / 1024) . " MB\r\n";
// Echo done
echo date('H:i:s') . " Done writing file.\r\n";
/**
* Creates a templated slide
*
* @param PHPPowerPoint $objPHPPowerPoint
* @return PHPPowerPoint_Slide
*/
function createTemplatedSlide(PHPPowerPoint $objPHPPowerPoint)
{
// Create slide
$slide = $objPHPPowerPoint->createSlide();
// Add background image
$shape = $slide->createDrawingShape();
$shape->setName('Background');
$shape->setDescription('Background');
$shape->setPath('./images/realdolmen_bg.jpg');
$shape->setWidth(950);
$shape->setHeight(720);
$shape->setOffsetX(0);
$shape->setOffsetY(0);
// Add logo
$shape = $slide->createDrawingShape();
$shape->setName('PHPPowerPoint logo');
$shape->setDescription('PHPPowerPoint logo');
$shape->setPath('./images/phppowerpoint_logo.gif');
$shape->setHeight(40);
$shape->setOffsetX(10);
$shape->setOffsetY(720 - 10 - 40);
// Return slide
return $slide;
}
Die Testdatei geniert folgende Konsolenausgabe:
23:16:10 Create new PHPPowerPoint object 23:16:10 Set properties 23:16:10 Remove first slide 23:16:10 Create templated slide 23:16:10 Create a shape (rich text) 23:16:10 Create templated slide 23:16:10 Create a shape (rich text) 23:16:10 Create a shape (rich text) 23:16:10 Create templated slide 23:16:10 Create a shape (rich text) 23:16:10 Create a shape (rich text) 23:16:10 Create templated slide 23:16:10 Create a shape (rich text) 23:16:10 Create a shape (rich text) 23:16:10 Create templated slide 23:16:10 Write to PowerPoint2007 format 23:16:10 Peak memory usage: 3.5 MB 23:16:10 Done writing file.
Und dieses PDF hab ich aus dem Ergebnis erstellt (dazu brauch man dann wieder Powerpoint 2007, das kann die Klasse glaube ich nicht
)
Soweit so gut.
Quassel – PHP_search
Ich nutze ja schon eine Weile lang Quassel-IRC. Leider durchsucht die Suche nur das bereits vorhandene Backlog (bzw. die "Chathistorie") und man muss immer erst mühsam bis zum Anfang hochscrollen, damit er sich "mehr" holt.
Jetzt hab ich einfach auf dem Server auf dem der Core läuft folgendes PHP-Script hinterlegt:
#!/usr/bin/php -q
<?php
$shortopts = "";
$shortopts .= "h:";
$shortopts .= "u:";
$shortopts .= "c:";
$shortopts .= "s:";
$options = getopt($shortopts);
function getMessage($db, $quasseluser, $channel, $search = null) {
$quasseluser = sqlite_escape_string($quasseluser);
$channel = sqlite_escape_string($channel);
$search = sqlite_escape_string($search);
$sql = '
SELECT DISTINCT
backlog.time AS time,
sender.sender AS sender,
backlog.message AS message,
buffer.buffername AS buffername
FROM backlog
OUTER JOIN quasseluser, buffer, sender
ON (
quasseluser.username = "'.$quasseluser.'"
AND
backlog.bufferid = buffer.bufferid
AND
quasseluser.userid = buffer.userid
AND
buffer.buffername LIKE "%'.$channel.'%"
AND
sender.senderid = backlog.senderid
)';
if ($search != null) {
$sql .= ' WHERE message LIKE "%'.$search.'%";';
}
$output = array();
$longestname = 0;
foreach ($db->query($sql) as $row) {
$sender = explode("!", $row['sender']);
$sendername = $sender[0];
if (strlen($sendername) > $longestname) {
$longestname = strlen($sendername);
}
array_push(
$output,
array(
date("Y-m-d H:i:s", $row['time']),
$row['buffername'],
$sendername,
$row['message']
));
}
foreach ($output as $entry) {
printf("%s (%s) %s : %s\n", $entry[0], $entry[1], str_pad($entry[2], $longestname, " "), $entry[3]);
}
printf("Es wurden %u Ergebnisse gefunden.\n", count($output));
}
try {
$homepath = $options["h"];
$username = $options["u"];
$buffer = $options["c"];
$search = $options["s"];
if(!$homepath || !$username || !$buffer) {
$output = "usage: ";
$output .= __FILE__." -h <path/to/quassel/sqlite> -u <quasseluser> -c <channel/buffer> [-s <string>]";
die($output . "\n");
}
$db = new PDO('sqlite:' . $homepath);
getMessage($db, $username, $buffer, $search);
} catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
}
Keinerlei Fehlerbehandlung, wenns schiefläuft geht bestimmt irgendwas irreperabel kaputt. Keine Garantie für nichts
Ein möglicher Aufruf wäre:
14:34:46 pennywise ~# ./searchQuassel.php -h quassel-storage.sqlite -u pennywise -c quassel.de -s foobar 2008-11-03 11:49:01 (#quassel.de) pennywise : und da war der Bot jetzt halt drin, weil er irgendwas gesagt hat wie "hallo foobar, wilkkommen in der hölle" 2008-11-06 20:35:57 (#quassel.de) pennywise : kann ich auch ein temporäres netzwerk haben? sowas wie /join irc.server.com/#foobar? 2008-11-12 11:52:56 (#quassel.de) hanswurst : im core-log steht nur "foobar benutzer x getrennt"
manpage
-h : Pfad zur SQLite-DB von quasselcore. Liegt meist in ~/.config/quassel-irc.org/
-u : Der Benutzer mit dem man sich in quassel einloggt. Dies hat nichts mit der IRC-Identität zu tun.
-c : Der Channel (bzw. das Query) in dem gesucht werden soll. Ist case insensitive und kann auch nur teilweise übergeben werden (z.B. "quassel" findet "#quassel", "#quassel.de" und "quassel-announce")
-s : OPTIONAL. Der Text nach dem gesucht werden soll. Wenn weggelassen, dann werden alle Nachrichten im übergebenen Channel zurückgegeben
Ich hatte auch noch ne Version mit Prepared Statements, aber die hat beim "$db->prepare($sql)" einfach kein Statement erzeugt. Ohne Fehlermeldung ohne alles. WTF?!
Scienta ZF Debug Bar
Der Firefox und seine diversen Erweiterungen. Es gibt mit Firebug und FirePHP schon exzellente Tools zur Entwicklung mit PHP und jetzt gibt es noch ein Weiteres:
Scienta ZF Debug Bar
Der aufmerksame Leser hat hier ZF entdeckt und, ja tatsächlich, hier ist die Rede vom Zend Framework.
Die Toolbar erlaubt es einem die SQL-Queries, die Page-Render-Zeit, die Version des ZF und den Speicherverbrauch anzuzeigen. Und noch ein paar Gimmicks mehr, dazu verweise ich aber einfach auf den Original-Blogeintrag.
Quelle: http://jokke.dk/blog/2009/01/introducing_the_scienta_zf_debug_bar
Professionell erstellte Supportanfragen
Gott sei Dank ist meine Supporterzeit schon ne Weile her, aber gerade hab ich eine Mailinglisten-Anfrage gelesen, da hab ich schon beim "Betreff"-Lesen das Schaudern gekriegt:
Noob configuration issue
Ohne Witz. Schon jetzt keine Lust auch nur annähernd mich in sein Problem reinzudenken
PHP hates me – eine Set-Klasse
Kommentare gesperrt, weil dieses Posting Spambots magisch anzieht.
Mir ist aufgefallen, daß meine Klasse nicht abfängt wenn man zweimal dasselbe Objekt ins Set setzt. Deshalb ein kleines Update
Nils Langner von PHP hates me hat eine Aufgabe gestellt. Und hier meine spontane Lösung, vom Hirn ins Terminal
<?php
<div class="moz-text-plain" style="font-family: -moz-fixed; font-size: 13px;" lang="x-western">
<pre>class Foobar {
}
/**
* @author Matthias Coy
*/
class Set {
private $set = null;
private $typ = null;
private $className = null;
/**
* The method to add something to the set. The type of the set is defined by its first entry.
* @param <type> $something
* @public
*/
function push($something) {
if ($this->set == null) {
$this->typ = gettype($something);
$this->set = array();
array_push($this->set, $something);
if($this->typ == "object") {
$class = new ReflectionClass($something);
$this->className = $class->getName();
echo "It's a class, with the name: '".$this->className."'\n";
} else {
echo "It's something else: '". $this->typ ."'";
}
} else {
$myType = gettype($something);
if($myType != $this->typ) {
throw new Exception("Wrong Type, this set is a set of '".$this->typ."'");
} else {
if ($myType == "object") {
$class = new ReflectionClass($something);
if ($this->className != $class->getName()) {
throw new Exception("Right Type, but wrong class (is: '".$class->getName()."', should be: '".$this->className."')");
}
foreach($this->set as $entry) {
if ($something === $entry) {
throw new Exception('Right Type, but already in Set');
}
}
}
array_push($this->set, $something);
echo "adding sth. (".$myType.")\n";
}
}
}
/**
* Return the last inserted entry of the set.
*
* @return <type>
* @public
* @throws Exception if set is empty
*/
function pop() {
$returnValue = array_pop($this->set);
if ($returnValue == null) {
$this->set = null;
$this->typ = null;
$this->className = null;
throw new Exception("Set is not yet defined, please add something first");
}
return $returnValue;
}
/**
* Returns the amount of entries in the set
*
* @return <type> Entry of the set
* @public
*/
function count() {
return count($this->set);
}
}
$var1 = new Set();
$var2 = new Foobar();
$mySet = new Set();
$mySet->push($var1);
try {
// Add something that is different to the types already in the set
$mySet->push($var2); // throws execption
} catch (Exception $e) {
print $e->getMessage()."\n";
}
try {
// Add something which has the right type, but is already inside the set
$mySet->push($var1); // throws execption
} catch (Exception $e) {
print $e->getMessage()."\n";
}
while($mySet->count() > 0) {
$mySet->pop();
}
$mySet->push($var2);
try {
$mySet->push($var1);
} catch (Exception $e) {
print $e->getMessage()."\n";
}
/* Output:
It's a class, with the name: 'Set'
Right Type, but wrong class (is: 'Foobar', should be: 'Set')
Right Type, but already in Set
It's a class, with the name: 'Foobar'
Right Type, but wrong class (is: 'Set', should be: 'Foobar')
*/
Netbeans 6.5
... ist inzwischen schon ne Weile draussen, aber jetzt hat sich mal jemand die PHP-Unterstützung genauer angeschaut: