Monday, September 17, 2007

.Php Framework: the .Net Framework clone in php

Downloads

.Php Framework - .Php Framework and a sample page



Introduction

I'm a .Net developer, I like the OOP and the .Net Framework structure.
In the last days I had to study and writing some web work with php.
Php results to me like a potential language but the syntax is awful, function names are all confusing and it is often used as a "spaghetti code".

This give me an idea on php usage: I copied the classes (ok, not all the classes :) ) of the .Net Framework in php.
This is to let me and other people using known classes and with a better syntax/OOP.

In example, array in php -> Dictionary in .Net
// php
// initialize the array with one item key/value
$my_array = array("color1" => "red");

// add others items
$my_array["color2"] = "blue";
$my_array["color3"] = "green";
// color2 was added?
$color_exists = array_key_exists("color2", $my_array);

// .Php Fw
// function to add the namespace
using("System.Collections");

// initialize the list with one item key/value
$list = new phpDictionary();
$list->Add("color1", "red");
$list->Add("color2", "blue");
$list->Add("color3", "green");
$color_exists = $list->ContainsKey("color2");
As you can see the .Php Framework code is similar to the .Net Framework code:
// C# .Net Fw
// function to add the namespace
using System.Collections.Generic;

// initialize the list with one item key/value
Dictionary list = new Dictionary();;
list.Add("color1", "red");
list.Add("color2", "blue");
list.Add("color3", "green");
bool color_exists = list.ContainsKey("color2");

.Php Fw Guidelines


Of course, php syntax is limited, so the copy of a class in php may be a little different fron the .Net one, but if I follow some guidelines the result is good.

  • Properties

    For php a property is a field of the class, so I divided a property in 2 way: the get and the set methods.
    • To get the value the method will have the plain name of the property.
    • To set the value the method will have the "Set" prefix and the parameter with the value.
    i.e for the Text property
    // The read way of a "property"
    function Text()
    {
    return $this->text;
    }

    // The writing way of a "property"
    function SetText(string $text)
    {
    $this->text = $text;
    }

Reference of .Php Fw


  • Next Release

    I hope to write something for the System.Data and I hope that someone can help me fixing bugs and enhance all the framework :)
  • Release 1.0

    Namespaces (incomplete! Not all the namespace classes are rewritten :) )
    + System
    + System.Collection
    + System.Diagnostics
    + System.IO
    + System.Reflection
    + System.Threading (not working: php does not support threading... :( )
    + System.Web
    + System.Web.UI

Notes

In System.Web.* I added quite ALL the web control fo ASP.Net: you can code a complete web page.
In the demo php in the package to download you can see the code to do this.
You can set it as the http://localhost and view it.

Php is funny, now it's also better and if you want to help me, or if you have some question don't be timid and ask to me ;)

Original Link : http://www.devbox4.net/?q=node/36

No comments:

About Me

Ordinary People that spend much time in the box
Powered By Blogger