Mostrando postagens com marcador PHP. Mostrar todas as postagens
Mostrando postagens com marcador PHP. Mostrar todas as postagens

Simple Groupware

"
SimpleGroupware
Simples e CMS Groupware é um groupware open source que oferece agenda, contatos, tarefas, emails, gerenciamento de conteúdo, gestão de documentos, a sincronização com telefones celulares e Outlook, assim como muitos mais recursos. Groupware é simples web é livre solução de groupware baseado em PHP, dirigido a particulares e usuários de negócios, traduzido para 21 línguas e está disponível sob a GPLv2.

sgs_quick_add.png


Entre um novo módulo para pesquisas, a nova versão recebeu um gerenciador de extensões. Com isso, as extensões podem ser instalados e não instalados de forma rápida e fácil com apenas 2 cliques. O download dos pacotes e da configuração do banco de dados é feito automaticamente. Para obter os pacotes, o sistema mundial de espelho amplo download do SourceForge.net é usado.



Além disso, a usabilidade foi melhorada, permitindo que as nomeações para ser criado com especificações de data relativa como "próxima sexta-feira 13:00. Utilizando o "Quick Add 'função, as nomeações podem ser criados com um forro de uma como' Meeting, 10:00, 11:00. Além disso, o layout foi adaptado para o ipad e seu controle sensível ao toque.

Creating a Web Poll with PHP

Polls are nearly ubiquitous on the web today, and there are plenty of services that will provide a drop-in poll for you. But what if you want to write one yourself? This tutorial will take you through the steps to create a simple PHP-based poll, including database setup, vote processing, and displaying the poll.





Step 1: Plan & Create the Database


In order to store poll results, we’re going to store three pieces of information:



  • A question identifier

  • An answer identifier

  • The number of votes a question/answer pair has gotten


For this tutorial, we’ll be using PDO and SQLite. If you’re working with SQLite3, you can create a new database via the command line tool; if you’re using an older version, a quick PHP script will do the trick. Here’s the one used for this tutorial:


<?php
echo "creating database\n";
try {
$dbh = new PDO('sqlite:voting.db');
$dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
$dbh->exec('
CREATE TABLE tally (
QID varchar(32) NOT NULL,
AID integer NOT NULL,
votes integer NOT NULL,
PRIMARY KEY (QID,AID))
');
}
catch(PDOException $e) {
echo "ERROR!!: $e";
exit;
}
echo "db created successfully.";
?>

Voting Database Structure

This simple script will create a SQLite database in the directory you run it. Unlike mySQL, the database here is a flat file. If you’re familiar with SQL, the create should make sense to you, although the last line may be new to some people:


PRIMARY KEY (QID,AID)

This creates a composite key for the database. Entries in either column do not have to be unique to that column, but the combination of the two must be unique.




Step 2: Design Your Poll’s HTML


Before you start writing any PHP, you need to decide how to create your poll in terms of markup. We’re going to try to keep the markup as semantic and simple as possible. Your poll will have two looks:



  • Question waiting to be answered

  • Current Results of the poll


In writing this HTML, some classes will be included to help with the CSS later.


Poll View


Because a poll is primarily a list of answers, we’re going to incorporate an unordered list to contain those answers. For the question itself, we’re going to use a heading tag.


<form class="webPoll" method="post" action="test.php">
<h4>What question would you like to ask?</h4>
<ul>
<li>Answer Here</li>
<li>Another Answer Here</li>
</ul>
</form>

That’s pretty basic, but doesn’t include any form elements. Radio buttons are the most appropriate since we’re only allowing one answer per poll. Also, we’re going to use the label tag to associate answers with the proper radio button. Now our form HTML looks more like this:


<form class="webPoll" method="post" action="test.php">
<h4>What question would you like to ask?</h4>
<ul>
<li>
<label class='poll_active'>
<input type='radio' name='AID' value='0'>
First Answer Here
</label>
</li>
</ul>
</form>

That’s a little more complex, but not too bad. Still a bit more to add. We’re going to include a fieldset tag to open up some styling options, and of course we need a submit button!


<form class="webPoll" method="post" action="/poll/test.php">
<h4>What question would you like to ask?</h4>
<fieldset>
<ul>
<li>
<label class='poll_active'>
<input type='radio' name='AID' value='0'>
First Answer Here
</label>
</li>
</ul>
</fieldset>
<p class="buttons">
<button type="submit" class="vote">Vote!</button>
</p>
</form>

For each answer, a new LI tag is added and the value of the radio button incremented. That will eventually be done by our PHP. The extra HTML is the fieldset tag, and the paragraph wrapped around the button – both of which will be used by our CSS.


Answer View


The HTML is going to be nearly identical for the answer view. The line-item tags won’t contain a form element and we’ll be adding a div which can be used to show the percentage of votes that answer received. Here’s how that will look:


<li>
<div class='result' style='width:20px;'>&nbsp;</div>
<label class='poll_results'>
10%: First Answer Here
</label>
</li>

Yes, that’s an inline style you see there. That style will be generated by our PHP based on the current percentage of each individual answer. Here’s what we have so far:


Unstyled Poll



Step 3: Style the Form


The HTML we created in the last step was not very attractive. Let’s see if we can fix that up a bit. We’re going to use the wonderful CSS3 PIE (progressive Internet Explorer) library so we can obtain a similar look across all browsers. To make this library work properly, there are numerous cases where you must apply a relative position to elements. You can read all the details on the library website.


Style the Form Tag


We’re going to use the form tag as our container. It’s going to have nice, rounded corners, and a bit of a drop shadow. The styles below also specify a width, and padding.


form.webPoll {
background:#ededed;
behavior:url(PIE.php);
border:1px solid #bebebe;
-moz-border-radius:8px;
-webkit-border-radius:8px;
border-radius:8px;
-moz-box-shadow:#666 0 2px 3px;
-webkit-box-shadow:#666 0 2px 3px;
box-shadow:#666 0 2px 3px;
margin:10px 0 10px 8px;
padding:6px;
position:relative;
width:246px;
}

The key line here is the behavior attribute. This will be ignored by non-IE browsers and will add the CSS3 functionality to IE6-8.


Basic styling

Still ugly, but a noticeable improvement.


Box the Answers


Next, we’re going to create a nice box around the answers and use a bit of illusion to make the border look inset by a pixel. This is done by coloring the outer-most border (the fieldset) the same color as the interior, and then using the unordered-list tag as our real border. Here’s the CSS:


form.webPoll fieldset {
background:#FCFAFC;
behavior:url(PIE.php);
border:1px solid #FCFAFC;
-moz-border-radius:10px;
-webkit-border-radius:10px;
border-radius:10px;
margin:0;
padding:0;
position:relative;
}
form.webPoll ul {
behavior:url(PIE.php);
border:2px #bebebe solid;
-moz-border-radius:10px;
-webkit-border-radius:10px;
border-radius:10px;
font-family:verdana;
font-size:10px;
list-style-type:none;
margin:0;
padding:10px 0;
position:relative;
}

Basic styling

Style the Answers


Next we need to add a little CSS to make our options look better.


form.webPoll li {
margin:0 16px;
overflow:auto;
padding:4px 0 6px;
position: relative;
}
form.webPoll input {
position: absolute;
top: 4px;
*top: 0;
left: 0;
margin: 0;
padding:0;
}
label.poll_active {
float:right;
width:90%;
}

You might ask why we’re using absolute positioning on the inputs and floating the label. The reason is simple: multi-line answers. If an answer to your poll question is long, you want the radio button to look like a bullet on an unordered list – hanging. This will keep the text from wrapping around it if it’s multiple lines.


There’s also a style targeting IE specifically with the * hack to cause the buttons to line up properly in IE6-8.


We also need to style the bar used to show results. We’ll add that now:


form.webPoll .result {
background: #d81b21;
background: -webkit-gradient(linear, left top, left bottom, from(#ff8080), to(#aa1317));
background: -moz-linear-gradient(top, #ff8080, #aa1317);
-pie-background: linear-gradient(#ff8080, #aa1317);
border:1px red solid;
-moz-border-radius:3px;
-webkit-border-radius:3px;
border-radius:3px;
clear:both;
color:#EFEFEF;
padding-left:2px;
behavior: url('PIE.php');
}

There’s another new attribute here: -pie-background, which allows us to, in conjunction with the PIE library, use gradient backgrounds in IE. There’s still a few touches to add.


Question and Button


A default H4 may not be what you’re looking for, so let’s add some styling to that.


form.webPoll h4 {
color:#444;
font-family:Georgia, serif;
font-size:19px;
font-weight:400;
line-height:1.4em;
margin:6px 4px 12px;
padding:0;
}

And I’m not a big fan of default buttons, so we’re going to use a CSS sprite to liven it up a bit.


.buttons {
margin:8px 0 1px;
padding:0;
text-align:right;
width:122px;
}
.vote {
background:url(res/vote.png) repeat scroll 0 0 transparent;
border:medium none;
height:40px;
text-indent:-9999em;
width:122px;
}
.vote:hover {
background-position:0 -41px;
cursor:pointer;
}

What about IE6? It doesn’t support the hover psudo-class! We can either leave those users out in the cold (they’ll still see the button default state) or we can use another lovely little GPL licensed library, Whatever:hover.


Final Poll CSS

Last Bits


In order to accommodate some IE6 quirks, certain elements need to have something called “HasLayout” triggered. The easiest way to do this is to set a property of zoom for these elements. The property is ignored by non-IE browsers.


form.webPoll ul,li { /*// Make IE6 happy //*/
zoom:1;
}

You’ll also notice there are borders between each question. This was done with an additional class on the LI tags specifying a border. The class will be assigned to all but the last item by the PHP script.


The completed CSS file is contained in the download.




Step 4: Create a PHP Class — Decide on the Interface


Now it’s time to create the PHP to generate polls, show results, and handle votes. I’d like to keep using the script as simple as possible, so I’m planning the usage ahead of time. To create a poll in a particular place in a page, you’ll just use the following PHP:


$a = new webPoll(array(
'What subjects would you like to learn more about?',
'HTML & CSS',
'JavaScript',
'JS Frameworks (jQuery, etc)',
'Ruby/Ruby on Rails',
'PHP',
'mySQL'));

That’s it. You’ll pass an array to the constructor which contains the question followed by the answers. In order to track the questions in the database, we’ll create an MD5 hash of the question to use as an identifier.




Step 5: Decide on the Class Properties


There’s certain data that’s going to be needed by each poll; we’re going to store some of this in class properties. We’ll need to store the question and the answers, the basic HTML, the question identifier, and some information on how to draw the results bars. Here’s the start:


class webPoll {

# makes some things more readable later
const POLL = true;
const VOTES = false;

# number of pixels for 1% on display bars
public $scale = 2;

# the poll itself
public $question = '';
public $answers = array();

# the HTML
private $header = '<form class="webPoll" method="post" action="%src%">
<input type="hidden" name="QID" value="%qid%" />
<h4>%question%</h4>
<fieldset><ul>';
private $center = '';
private $footer = "\n</ul></fieldset>%button%\n</form>\n";
private $button = '<p class="buttons"><button type="submit" class="vote">Vote!</button></p>';

# question identifier
private $md5 = '';

The initial constants will be used in one of the methods to make it more readable so it’s easier to know whats going on.


Take note of the hidden input that’s been added here. This is the question identifier used to store information in the database. All the values in the HTML surrounded by percent signs will be replaced.




Step 6: Create the HTML Poll or Answers


Since it’s already been decided the poll will be made by creating an object, let’s examine the __construct method.


public function __construct($params) {
$this->question = array_shift($params);
$this->answers = $params;
$this->md5 = md5($this->question);

$this->header = str_replace('%src%', $_SERVER['SCRIPT_NAME'], $this->header);
$this->header = str_replace('%qid%', $this->md5, $this->header);
$this->header = str_replace('%question%', $this->question, $this->header);

# has the user voted yet?
isset($_COOKIE[$this->md5]) ? $this->poll(self::VOTES) : $this->poll(self::POLL);
}

In the first line, we peel the question off the array stack with array_shift, and store it in a property. We also store the questions, leaving them as an array. We also create the question identifier here, by making an md5 hash of the question itself.


The next three lines perform some replacements on the HTML. The first sets our form action to point at the page the poll is one. The second puts our question identifier in a hidden form field. The third puts our question into the HTML.


In the final line of the constructor we check if the user has voted on this particular poll, and if he has, we show the votes. If he hasn’t, we show the poll.




Step 7: Generate the Poll


Both generating the poll and generating the results are very similar operations. In order to keep our code DRY we break the creation into three methods. The main one is “poll”.


DRY: Don't Repeat Yourself

private function poll($show_poll) {
$replace = $show_poll ? $this->button : '';
$this->footer = str_replace('%button%', $replace, $this->footer);

# static function doesn't have access to instance variable
if(!$show_poll) {
$results = webPoll::getData($this->md5);
$votes = array_sum($results);
}

for( $x=0; $x<count($this->answers); $x++ ) {
$this->center .= $show_poll ? $this->pollLine($x) : $this->voteLine($this->answers[$x],$results[$x],$votes);
}

echo $this->header, $this->center, $this->footer;
}

Here’s the breakdown of what’s going on in this function:


lines 2 & 3: We only need a vote button if the user hasn’t voted. Here we determine if we’re going to use the button HTML or not, and then either insert the HTML, or replace the %button% placeholder with an empty string.


lines 6 – 8: If we’re not showing the poll, we obviously need the results, so here we go fetch them. We also calculate the total votes cast for use later in determining percentages.


lines 11 – 12: This generates the LI tags in our HTML. Depending on if we’re showing the poll or the results, we generate different HTML. This HTML generation is handed off to two functions:



  • pollLine

  • voteLine


line 15: Simply dumps out the data to the page.




Step 8: The pollLine() Method


This is a very simple method, which takes the current index of the answer as an argument.


private function pollLine($x) {
isset($this->answers[$x+1]) ? $class = 'bordered' : $class = '';
return "
<li class='$class'>
<label class='poll_active'>
<input type='radio' name='AID' value='$x' />
{$this->answers[$x]}
</label>
</li>
";
}

It checks if there’s an answer after the current one on its first line, and if there is, applies a class of bordered to that LI tag. The very last answer won’t get this class, allowing us to achieve the visual effect intended.




Step 9: The voteLine() Method


This method is getting 3 parameters passed into it:



  • $answer : The question answer for this line

  • $result : The number of votes this option has gotten

  • $votes : The total number of votes cast in this poll


With that information, the LI tags for the voting results can be produced.


private function voteLine($answer,$result,$votes) {
$result = isset($result) ? $result : 0;
$percent = round(($result/$votes)*100);
$width = $percent * $this->scale;
return "
<li>
<div class='result' style='width:{$width}px;'>&nbsp;</div>{$percent}%
<label class='poll_results'>
$answer
</label>
</li>
";
}

Since it’s possible for there to be no votes for an option, it will effectively leave $result unset. If we detect this we’ll give it a default value of 0 votes.


Next, we determine what percent of the votes the option got and finally use the scale property to determine the width, in pixels, that the results bar should be. Then we finally return the HTML containing all that information.




Step 10: Write the getData() Method


If you look back up a bit, you’ll see we call the getData() method which is defined as a static method in the class. Why static? Because if we decide to enhance this poll later by making it AJAX based, we’ll want access to that method without object creation. Here’s the method:


static function getData($question_id) {
try {
$dbh = new PDO('sqlite:voting.db');
$dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );

$STH = $dbh->prepare('SELECT AID, votes FROM tally WHERE QID = ?');
$STH->execute(array($question_id));
}
catch(PDOException $e) {
# Error getting data, just send empty data set
return array(0);
}

while($row = $STH->fetch()) {
$results[$row['AID']] = $row['votes'];
}

return $results;
}

The question ID is passed into the method and it will return an array containing the answer ID’s and the number of votes that answer has. If an answer has no votes, it won’t have an entry in the array, which we’ve already dealt with in the voteLine() method.


Since database errors in web polls are particularly tragic, we’re simply going to return an empty array if one occurs. The user will get 0 votes for each result. In a production environment you might want to log this error to a file, or send the admin an email.




Step 11: Handling a Vote


We’re going to add a second static method to the class, and this one will handle incoming votes. Votes will only be counted if the user hasn’t voted before (as determined by a cookie) and once the user has voted we’ll set a cookie indicating this.


In this type of web application, it’s nearly impossible to stop multiple votes without excluding some legitimate users. Setting a cookie is just a basic precaution.


This is one of the more complex methods in our webPoll class, and we’re going to look at it in three parts.


static function vote() {
if(!isset($_POST['QID']) ||
!isset($_POST['AID']) ||
isset($_COOKIE[$_POST['QID']])) {
return;
}

A call to the vote() method will be at the top of our PHP page, so the first thing we want to do is decide if there’s a vote to process or not. The above statement is how we determine this. Here’s what it says:



  • If there’s no Question Identifier in our POST data (OR!!)

  • If there’s no Answer Identifier in our POST data (OR!!)

  • If a cookie has been set already matching the Question Identifier


If any of those are true, we don’t have to process a vote, and we leave the method.


$dbh = new PDO('sqlite:voting.db');
$dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );

try {
$sth = $dbh->prepare( "INSERT INTO tally (QID,AID,votes) values (:QID, :AID, 1)" );
$sth->execute(array($_POST['QID'],$_POST['AID']));
}
catch(PDOException $e) {
# 23000 error code means the key already exists, so UPDATE!
if($e->getCode() == 23000) {
try {
$sth = $dbh->prepare( "UPDATE tally SET votes = votes+1 WHERE QID=:QID AND AID=:AID");
$sth->execute(array($_POST['QID'],$_POST['AID']));
}
catch(PDOException $e) {
$this->db_error($e->getMessage());
}
}
else {
$this->db_error($e->getMessage());
}
}

This looks a lot more complicated then it really is. What happens here is we check if a particular answer has gotten a vote before. If it hasn’t we create a new record for that answer, and give it one vote. If it has, we update the existing record. So how’s it decide which to do?


PDO exception magic.


Remember at the very beginning we created our multi-column primary key? When we try to insert a record into the table which matches an existing QID/AID pair, an exception is thrown, and in particular the exception code is 23000 (duplicate key).


If the insert throws an exception, we’re going to check the exception code, and if it matches 23000, we’ll try to update the record instead. Of course if the insert fails for a different reason, or the update fails as well, we’re going to just issue a call to a method called db_error() which just echos a generic error message. Like before, a production environment would log this error and/or notify the admin.


Using PDO Exceptions

Finally, the end of the method:


    # entry in $_COOKIE to signify the user has voted, if he has
if($sth->rowCount() == 1) {
setcookie($_POST['QID'], 1, time()+60*60*24*365);
$_COOKIE[$_POST['QID']] = 1;
}
}

By using rowCount() we can verify that we’ve either updated or inserted a vote. If a vote has been successfully registered we set a cookie indicating as much, using the question identifier as the cookie name.


In addition to setting the cookie, we populate the super-global $_COOKIE, so when the poll displays, it shows answers rather then presenting the poll again.




Step 12: Put It All Into Action


We’ve written the PHP, set up the CSS and HTML, now it’s time to put it all into use. In this example, we’re just going to drop everything into a page that’s otherwise blank. At the very top of the page, insert the following:


<?php
include('webPoll.class.php');
webPoll::vote();
?>

It’s important that this be the very top of the page, before any HTML. Why? Because if there’s a vote to process, a cookie may be written, and you can’t write cookies after anything else has been sent. The call to the static method vote() returns if there’s not the proper POST data to process.


Next, we’ll include all the styles we wrote as a seperate stylesheet. Also, we’re going to include a particular style just for IE that was mentioned earlier to enable the :hover psudo-class.


<link rel="stylesheet" href="poll.css" type="text/css" />
<!--[if IE]>
<style> body { behavior: url("res/hover.htc"); } </style>
<![endif]-->

In the BODY of your HTML page, you’ll drop in the following PHP to insert the polls:


$a = new webPoll(array(
'What subjects would you like to learn more about?',
'HTML & CSS',
'JavaScript',
'JS Frameworks (jQuery, etc)',
'Ruby/Ruby on Rails',
'PHP',
'mySQL'));

$b = new webPoll(array(
'What is your question?',
'Don\'t have one',
'Why?',
'When?',
'Where?'));

That’s it! Thanks for reading. Any thoughts, questions, or suggestions?


Compreender e aplicar Polimorfismo em PHP

Na programação orientada a objetos, polimorfismo é uma ferramenta poderosa e fundamental. Ele pode ser usado para criar um fluxo mais orgânica em sua aplicação. Este tutorial irá descrever o conceito geral de polimorfismos, e como ele pode ser facilmente implementado em PHP.


O que é polimorfismo?

Polimorfismo é uma palavra muito tempo para um conceito muito simples.

Polimorfismo descreve um padrão de programação orientada a objeto em que as classes têm diferentes funcionalidades ao compartilhar de uma interface comum.

A beleza do polimorfismo é que o código de trabalho com as diferentes classes, não precisa saber qual a classe que está usando desde que todos eles são usados da mesma forma.

A analogia com o mundo real para o polimorfismo é um botão. Toda a gente sabe como usar um botão: você simplesmente pressionar a ele. O que um botão "não", entretanto, depende do que ele está conectado e do contexto em que ela é usada - mas o resultado não afeta a forma como é utilizado. Se o seu patrão lhe diz para pressionar um botão, você já tem todas as informações necessárias para executar a tarefa.

No mundo da programação, polimorfismo é usado para fazer aplicações mais modular e extensível. Em vez de declarações condicionais bagunçado descrevendo os diferentes cursos de ação, você cria objetos intercambiáveis que você selecionar com base em suas necessidades. Esse é o objetivo básico de polimorfismo.


Interfaces

Uma parte integrante do polimorfismo é a interface comum. Existem duas maneiras de definir um interface em PHP: interfaces e classes abstratas. Ambos têm seus usos, e você pode misturar e combinar-los como quiser em sua hierarquia de classe.

Usando uma interface

Uma interface é similar a uma classe, exceto que ele não pode conter código. Pode definir nomes de métodos e argumentos, mas não o conteúdo dos métodos. Todas as classes que implementam uma interface deve implementar todos os métodos definidos pela interface. Uma classe pode implementar várias interfaces.

Uma interface é declarado usando a ' interface 'palavra-chave:

 (interface MyInterface
/ / Métodos
)

e está associado a uma classe usando o ' implements 'palavra-chave:

 MyClass classe implementa (MyInterface
/ / Métodos
)

Métodos podem ser definidos na interface, tal como em uma classe, exceto sem o corpo (a parte entre as chaves):

 (interface MyInterface
DoThis função pública ();
doThat função pública ();
setName função pública ($ name);
)

Todos os métodos definidos aqui terá de ser incluído em qualquer implementação de classes exatamente como descrito. (Nota: os comentários de código abaixo.)

 / / Válido
MyClass classe implementa (MyInterface
$ nome pública;
público DoThis function () (
/ / Código que faz isso
)
doThat função pública () (
/ / Código que faz isso
)
setName função pública ($ name) (
$ This-> nome = $ nome;
)
)

/ / Inválido
MyClass classe implementa (MyInterface
/ / DoThis falta ()!
doThat função pública () (
/ / Este deve ser público!
)
público setName function () (
/ / O argumento de falta nome!
)
)

Usando uma classe abstrata

Uma classe abstrata é uma mistura entre uma interface e uma classe. Pode definir a funcionalidade, bem como interface (na forma de métodos abstratos). Classes estender uma classe abstrata deve implementar todos os métodos abstratos definidos na classe abstrata.

Uma classe abstrata é declarada da mesma forma que as classes com a adição do " abstract palavra-chave ":

 MyAbstract classe abstrata (
/ / Métodos
)

e está associado a uma classe usando o ' extends 'palavra-chave:

 Class MyClass (estende MyAbstract
/ / Métodos da classe
)

métodos regulares podem ser definidos em uma classe abstrata, tal como em uma classe regular, bem como de quaisquer métodos abstratos (usando o ' abstract 'palavra-chave). métodos abstratos se comportam exatamente como os métodos definidos em uma interface, e deve ser implementado exatamente como definido pela extensão classes.

 MyAbstract classe abstrata (
protected $ nome;
público DoThis function () (
/ / Fazer isso
)
resumo doThat função privada ();
resumo setName função pública ($ name);
)

Passo 1: identificar o problema

Vamos imaginar que você tem um Article classe que é responsável pela gestão de artigos em seu site. Ele contém informações sobre um artigo, incluindo o título, autor, data e categoria. Aqui está o que parece:

 poly_base_Article classe (
$ titulo pública;
autor $ público;
$ date pública;
categoria $ público;

public function __construct ($ title, $ autor, data, $ categoria = 0) (
$ This-> title = $ title;
$ This-> author = $ autor;
$ This-> data = $ data;
$ This-> categoria = $ categoria;
)
)

Nota: As classes de exemplo neste tutorial usa a convenção de nomenclatura de "package_component_Class." Esta é uma maneira comum de classes separadas em namespaces virtuais para evitar colisões de nomes.

Agora você quer adicionar um método para a produção da informação em diferentes formatos, como XML e JSON. Você pode estar tentado a fazer algo como isto:

 poly_base_Article classe (
//...
public function escreve ($ type) (
''Ret = $;
switch ($ tipo) (
caso 'XML':
$ ret = '<article>;
$ ret .= '<title>. título $ obj->. '</ Title> ";
$ ret .= '<author>. autor $ obj->. '</ Autor>';
$ ret .= '<data>. data $ obj->. '</ Data>';
$ ret .= '<category>. categoria $ obj->. "</> Categoria ';
$ ret .= '</ artigo>';
break;
case 'JSON':
array $ array = ('artigo' => $ obj);
$ Ret = json_encode ($ array);
break;
)
return $ ret;
)
)

Esta é uma espécie de solução feio, mas funciona - por agora. Pergunte a si mesmo o que acontece no futuro, no entanto, quando queremos adicionar mais formatos? Você pode continuar editando a classe, adicionando mais e mais casos, mas agora você só está diluindo sua classe.

Um princípio importante da OOP é que uma classe deve fazer uma coisa, e deve fazê-lo bem.

Com isto em mente, as declarações condicional deve ser uma bandeira vermelha, indicando que a classe está tentando fazer muitas coisas diferentes. Este é o lugar onde o polimorfismo vem dentro

No nosso exemplo, é claro que existem duas tarefas apresentadas: artigos de gestão e formatação de seus dados. Neste tutorial, vamos refazer a nossa vontade de formatação de código em um novo conjunto de classes e descubra como é fácil utilizar polimorfismo.


Passo 2: Defina sua interface

A primeira coisa que devemos fazer é definir a interface. É importante pensar muito sobre a sua interface, pois qualquer alteração pode exigir alterações para o código de chamada. Neste exemplo, vamos estar usando uma interface simples para definir o nosso método um:

 poly_writer_Writer (interface
public function escreve (poly_base_Article $ obj),
)

É tão simples, temos um público definido write() método que aceita um objeto como um argumento do artigo. Qualquer classe que implementa a interface do escritor ter a certeza de que este método.

Dica: Se você quiser restringir o tipo de argumentos que podem ser passados para as funções e métodos, você pode usar as dicas, tipo como fizemos no write() método, que só aceita objetos do tipo poly_base_Article . Infelizmente, o tipo de retorno insinuando não é suportado nas versões atuais do PHP, por isso é até você para tomar conta de valores de retorno.


Passo 3: Criar a sua implementação

Com sua interface definida, é hora de criar as classes que realmente fazem coisas. No nosso exemplo, temos dois formatos que queremos para a saída. Assim, temos duas classes Escritor: XMLWriter e JSONWriter. Cabe a estes para extrair os dados do objeto do artigo passou e formatar as informações.

Aqui está o que parece XMLWriter:

 poly_writer_XMLWriter classe implementa (poly_writer_Writer
public function escreve (poly_base_Article $ obj) (
$ ret = '<article>;
$ ret .= '<title>. título $ obj->. '</ Title> ";
$ ret .= '<author>. autor $ obj->. '</ Autor>';
$ ret .= '<data>. data $ obj->. '</ Data>';
$ ret .= '<category>. categoria $ obj->. "</> Categoria ';
$ ret .= '</ artigo>';
return $ ret;
)
)

Como você pode ver a partir da declaração da classe, nós usamos a implements -chave para implementar a nossa interface. O write() método contém funcionalidades específicas para a formatação XML.

Agora aqui é a nossa classe JSONWriter:

 poly_writer_JSONWriter classe implementa poly_writer_Writer (
public function escreve (poly_base_Article $ obj) (
array $ array = ('artigo' => $ obj);
json_encode retorno ($ array);
)
)

Todos os nossos código específico para cada formato agora está contida dentro de classes individuais. Essas classes têm cada um a responsabilidade de lidar com um formato específico, e nada mais. Nenhuma outra parte do seu aplicativo precisa se preocupar com a forma como estes trabalhos, a fim de usá-lo, graças à nossa interface.


Etapa 4: Use a sua implementação

Com a nossa nova classe definida, é hora de rever a nossa classe artigo. Todo o código que viveu no original write() método tem sido fatorado em nosso novo conjunto de classes. Todos os nosso método tem a fazer agora é usar as novas classes, como este:

 poly_base_Article classe (
//...
public function escreve ($ poly_writer_Writer escritor) (
retorno escrever $ escritor-> ($ this);
)
)

Todos esse método não é agora aceitar um objeto da classe Writer (ou seja, qualquer classe que implementa a interface do Writer), o seu apelo write() método, passando-se ( $this ) como argumento, em seguida, encaminhar o valor de retorno direto para o cliente código. Ele não precisa mais se preocupar com os detalhes da formatação de dados, e pode se concentrar em sua tarefa principal.

Obtenção de um escritor

Você pode estar se perguntando onde você obtém um objeto Writer, para começar, pois você precisa passar uma a este método. Isso é até você, e há muitas estratégias. Por exemplo, você pode usar uma classe de fábrica para pegar dados do pedido e criar um objeto:

 poly_base_Factory classe (
getWriter função pública estático () (
/ / Pega variável pedido
formato formato = $ _REQUEST ['];
/ / Construir o nosso nome da classe e verificar a sua existência
$ class = "poly_writer_. formato $. "Escritor";
if (class_exists ($ class)) (
/ / Retorna um objeto novo escritor
novo retorno $ (classe);
)
/ / Caso contrário, não
throw new Exception ("formato não suportado");
)
)

Como eu disse, existem muitas outras estratégias para usar dependendo de suas necessidades. Neste exemplo, uma variável de solicitação escolhe qual o formato a utilizar. Ele constrói um nome de classe da variável pedido, verifica se ela existe, em seguida, retorna um objeto novo escritor. Se não existe nenhum com esse nome, uma exceção é acionada para deixar o código do cliente descobrir o que fazer.


Passo 5: Put It All Together

Com tudo em seu lugar, aqui é como o nosso código do cliente que juntar tudo isso:

 artigo = $ poly_base_Article novo ('polimorfismo "," Steve "tempo, (), 0);

try (
escritor $ = poly_base_Factory: getWriter ();
)
catch (Exception $ e) (
escritor poly_writer_XMLWriter $ = new ();
)

echo $ artigo-> write ($ escritor);

Primeiro criamos um artigo exemplo de objeto para trabalhar com ele. Então, tentamos obter um objeto Writer da fábrica, caindo de volta a um padrão (XMLWriter) se uma exceção é lançada. Finalmente, passar o objeto de nosso artigo do escritor de write() método, a impressão do resultado.


Conclusão

Neste tutorial, eu tenho desde que com uma introdução ao polimorfismo e uma explicação de interfaces em PHP. Eu espero que você perceber que eu só mostrei um caso de uso potencial para o polimorfismo. Há muitas, muitas outras aplicações. O polimorfismo é uma maneira elegante de escapar feio declarações condicionais no código POO. Ele segue o princípio da conservação dos seus componentes em separado, e é parte integrante dos padrões de design. Se você tiver alguma dúvida, não hesite em perguntar nos comentários!

PHP para Android projeto iniciado

php4-android
irontec acabam de lançar um projeto open source PHP para trazer a plataforma Android. PHP para Android projeto (PFA), visa tornar o desenvolvimento PHP no Android não só possível, mas também possível fornecer ferramentas e documentação. O projeto já tem um APK que fornece suporte ao Android PHP Scripting Ambiente (ASE). Para começar, você pode seguir o screencast abaixo:



APK e código fonte disponível tanto na http://phpforandroid.net. Requisito mínimo para obter o PHP para o Android rodando Android é 1,5 telefone ou emulador. Há ainda um ASE oficial construir com PHP 5.3 suporte incluído. Agora Rasmus podem obter o telefone do Android e iniciar o script no celular.


[AJAX Magazine] phpVirtualBox, Manage your VirtualBox with Ajax and PHP

Oracle VM VirtualBox - software de virtualização a final para a empresa e em casa - veio com um instrumento chamado vboxwebsrv webservice, e se você tem o seu VirtualBox instalado em uma máquina host você não tem necessariamente na mesma máquina para gerenciá-lo. phpVirtualBox é um novo projeto que visa tornar a gestão do VirtualBox possível simplesmente de um browser web. A aplicação web é powered by PHP e jQuery proporcionando um clone real do software VirtualBox no seu navegador web. Muito do seu verbage e alguns de seus códigos é baseada na (inativo) vboxweb projeto.



phpvirtualbox


phpVirtualBox requer um servidor web com PHP> = 5.2.0 e um trabalho> Instalação = VirtualBox 3.2.2. As características-chave incluem:



- Start / Stop VMs: Pause, Save Estado, Descarte Estado, Power Off, Sleep ACPI, ACPI Shutdown

- Instantâneos: Take Snapshot, Snapshot Excluir, Restaurar Snapshot

- Ver VM arquivos de log

- Configure VMs: Exclui a configuração da porta serial, exclui algumas opções de exibição que não afetam um ambiente sem cabeça

- Mídia: Adic, Full featured gerenciador de mídia virtual, assistente Criar disco rígido, mídia Monte read-only (DVD / CD e imagens de disquete), enquanto a VM está rodando, Clone imagem de disco, faz imagem de disco imutável (somente leitura)

- Criar máquina virtual assistente

- Configuração de rede global: Adicionar / remover / configurar interfaces host-only, configurações DHCP para configurar interfaces host-only

- Import / Export Máquinas Virtuais

- Virtual Machine acesso Console: Permite a especificação do nome de usuário e senha e resolução de tela, pode ser "separada" para abrir a VM do console em um mínimo janela do navegador, novo.



VirtualBox OSE é suportado mas o suporte para o console guia, USB e VRDP será deixado claro. Observe que o script não executa autenticação front-end de qualquer espécie, que se destina a ser executado em uma rede local ou intranet, onde o acesso ao script phpVirtualBox é limitada pela conectividade de rede. Lançado sob a GNU General Public License v3.

ORM Designer Reviewed

Muitos desenvolvedores criar aplicativos da Web acessar bancos de dados não pode imaginar o trabalho sem usar o quadro ORM, mas tanto quanto os quadros ORM facilitar o desenvolvimento e tornar o código mais fácil de ler que também pode levar muito tempo criação. Alguns dos melhores frameworks ORM em PHP são Doutrina e Propel CakePHP. Todos estes frameworks são baseados em torno do banco de dados e definições ORM escrito pelo desenvolvedor. Isto dá ao desenvolvedor uma maior liberdade para otimizar a forma como o quadro ORM trabalha com o banco de dados, mas também significa ainda mais um código para manter. Designer ORM é um novo software que ajuda os desenvolvedores com as tarefas relacionadas à criação e manutenção de definições ORM.



Symfony Jobeet Doctrine


O que é Designer ORM?



Se você ver Designer ORM, pela primeira vez, você pode pensar que é outra ferramenta de modelagem de ERD. Depois de ter um segundo olhar você vai perceber que há muito mais para o Designer ORM e modelagem ERD é apenas a de muitas ferramentas que não foram vistos em qualquer outro software que faz antes e Designer ORM único. Vamos ter um olhar que não oferecem Designer ORM ao lado de modelagem visual. Uma das características principais é a importação sofisticados recursos / exportação de e para os quadros ORM, MySQL Workbench ou DBDesigner fabForce. Isto é muito útil quando se utiliza Designer ORM no projeto existente. Actualmente os suportados são frameworks ORM Doctrine, Doutrina 2 Propel e CakePHP, mas mais devem vir no futuro próximo. Os autores prometem apoiar qualquer quadro ORM que poderia prestar a cooperação na implementação definições ORM.