Garry Lachman ActionScript 3 / Flex / Php & mySQL Blog

9Jun/100

Migrating from PHP 4 to PHP 5 Presentation

Migrating from PHP 4 to PHP 5 by Derick Rethans.
Really good one, just give a look...

http://talks.php.net/show/migrating-lt2005

Have Fun ;)
Garry Lachman

VN:F [1.9.0_1079]
Rating: 10.0/10 (1 vote cast)
VN:F [1.9.0_1079]
Rating: +1 (from 1 vote)
  • Share/Bookmark
26Apr/100

PHP MySQL Shopping Cart Tutorial in PHP

Very good & useful tutorial that i found while search the web about
Abstracting of shopping cart....

"Yes, this is a another shopping cart tutorial. I am planning to make this tutorial to cover a more sophisticated shopping cart solution but for now it only explains a basic shopping cart. I will improve it in time so stay tuned."

Check if out here: http://www.phpwebcommerce.com/

Have fun,
Garry Lachman

UF4W22PSSX9C

VN:F [1.9.0_1079]
Rating: 10.0/10 (1 vote cast)
VN:F [1.9.0_1079]
Rating: +2 (from 2 votes)
  • Share/Bookmark
21Apr/100

Base64 Encoding Class in ActionScript 3

This is a class for Base64 Encoding by Steve Webster...
Very Usefull one!!!

Base64.as

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package {
/*
Base64 - 1.1.0

Copyright (c) 2006 Steve Webster

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/


package {

        import flash.utils.ByteArray;
       
        public class Base64 {
               
                private static const BASE64_CHARS:String = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";

                public static const version:String = "1.1.0";

                public static function encode(data:String):String {
                        // Convert string to ByteArray
                        var bytes:ByteArray = new ByteArray();
                        bytes.writeUTFBytes(data);
                       
                        // Return encoded ByteArray
                        return encodeByteArray(bytes);
                }
               
                public static function encodeByteArray(data:ByteArray):String {
                        // Initialise output
                        var output:String = "";
                       
                        // Create data and output buffers
                        var dataBuffer:Array;
                        var outputBuffer:Array = new Array(4);
                       
                        // Rewind ByteArray
                        data.position = 0;
                       
                        // while there are still bytes to be processed
                        while (data.bytesAvailable > 0) {
                                // Create new data buffer and populate next 3 bytes from data
                                dataBuffer = new Array();
                                for (var i:uint = 0; i < 3 && data.bytesAvailable > 0; i++) {
                                        dataBuffer[i] = data.readUnsignedByte();
                                }
                               
                                // Convert to data buffer Base64 character positions and
                                // store in output buffer
                                outputBuffer[0] = (dataBuffer[0] & 0xfc) >> 2;
                                outputBuffer[1] = ((dataBuffer[0] & 0x03) << 4) | ((dataBuffer[1]) >> 4);
                                outputBuffer[2] = ((dataBuffer[1] & 0x0f) << 2) | ((dataBuffer[2]) >> 6);
                                outputBuffer[3] = dataBuffer[2] & 0x3f;
                               
                                // If data buffer was short (i.e not 3 characters) then set
                                // end character indexes in data buffer to index of '=' symbol.
                                // This is necessary because Base64 data is always a multiple of
                                // 4 bytes and is basses with '=' symbols.
                                for (var j:uint = dataBuffer.length; j < 3; j++) {
                                        outputBuffer[j + 1] = 64;
                                }
                               
                                // Loop through output buffer and add Base64 characters to
                                // encoded data string for each character.
                                for (var k:uint = 0; k < outputBuffer.length; k++) {
                                        output += BASE64_CHARS.charAt(outputBuffer[k]);
                                }
                        }
                       
                        // Return encoded data
                        return output;
                }
               
                public static function decode(data:String):String {
                        // Decode data to ByteArray
                        var bytes:ByteArray = decodeToByteArray(data);
                       
                        // Convert to string and return
                        return bytes.readUTFBytes(bytes.length);
                }
               
                public static function decodeToByteArray(data:String):ByteArray {
                        // Initialise output ByteArray for decoded data
                        var output:ByteArray = new ByteArray();
                       
                        // Create data and output buffers
                        var dataBuffer:Array = new Array(4);
                        var outputBuffer:Array = new Array(3);

                        // While there are data bytes left to be processed
                        for (var i:uint = 0; i < data.length; i += 4) {
                                // Populate data buffer with position of Base64 characters for
                                // next 4 bytes from encoded data
                                for (var j:uint = 0; j < 4 && i + j < data.length; j++) {
                                        dataBuffer[j] = BASE64_CHARS.indexOf(data.charAt(i + j));
                                }
                       
                        // Decode data buffer back into bytes
                                outputBuffer[0] = (dataBuffer[0] << 2) + ((dataBuffer[1] & 0x30) >> 4);
                                outputBuffer[1] = ((dataBuffer[1] & 0x0f) << 4) + ((dataBuffer[2] & 0x3c) >> 2);                
                                outputBuffer[2] = ((dataBuffer[2] & 0x03) << 6) + dataBuffer[3];
                               
                                // Add all non-padded bytes in output buffer to decoded data
                                for (var k:uint = 0; k < outputBuffer.length; k++) {
                                        if (dataBuffer[k+1] == 64) break;
                                        output.writeByte(outputBuffer[k]);
                                }
                        }
                       
                        // Rewind decoded data ByteArray
                        output.position = 0;
                       
                        // Return decoded data
                        return output;
                }
               
                public function Base64() {
                        throw new Error("Base64 class is static container only");
                }
        }
}

Useage:

1
2
3
4
5
var aString:String = "to Convert";
trace(Base64.encode(aString)); // Base64 String

var aBase64:String = "dG8gQ29udmVydA==";
trace(Base64.decode(aBase64)) // Original String

Have Fun ;)

VN:F [1.9.0_1079]
Rating: 10.0/10 (2 votes cast)
VN:F [1.9.0_1079]
Rating: +2 (from 2 votes)
  • Share/Bookmark
15Apr/100

CONFIRMED: Facebook Gets Faster, Debuts Homegrown PHP Compiler

Mike Melanson write on readwriteweb.com "The rumors have been flying over what's going on over at Facebook headquarters. The word has been that a PHP team was brought in and made to sign non-disclosure agreements before discussing a PHP project that has been in development for the past two years. Alex Handy, senior editor of the Software Development Times Blog, predicted last Saturday that Facebook "has rewritten the PHP runtime from scratch," and several sources have confirmed for us tonight that Facebook has indeed been making some changes to the basic PHP runtime environment.

According to our sources, Facebook has been working on a PHP compiler that will
increase speed by around 80% and offer a just-in-time (JIT) compilation engine that will offer a number of advantages. The project is very similar to Google's Unladen Swallow project, which rebuilt the Python compiler, boosting the speed fivefold and opening the door for multi-language integration."

More on readwriteweb.com

VN:F [1.9.0_1079]
Rating: 0.0/10 (0 votes cast)
VN:F [1.9.0_1079]
Rating: 0 (from 0 votes)
  • Share/Bookmark
14Apr/100

Get Vars from URL string in ActionScript 3

The function is break the string into param name and param value of the URL.
if we have URL like http://garry-lachman.com/index.php?name1=value1&test=10

The function will return array:
Array["name1"] = "value1";
Array["test"] = "10";

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public function getParamsFromURL(fullurl:String):Array
{
    var params:Array = fullurl.split('?');
    fullurl = params[1];
    params = fullurl.split('&');
    var length:uint = params.length;
    var g:uint;
    for (g=0; g< length; g++){
        var index:int=0;
        var kvPair:String = params[g];
        if((index = kvPair.indexOf("=")) > 0){
            var key:String = kvPair.substring(0,index);
            var value:String = kvPair.substring(index+1);
            params[key] = value;
        }
    }
    return params;
}

Have Fun ;)
Garry Lachman

VN:F [1.9.0_1079]
Rating: 0.0/10 (0 votes cast)
VN:F [1.9.0_1079]
Rating: 0 (from 0 votes)
  • Share/Bookmark
12Apr/102

Factory Design Pattern in PHP5

Factory Method return you the specific object by params he gets.
Lets make an example... we got 2 different classes for students that extends StudentAbstract class,
now we can make factory class that return the class by param.

StudentAbstract.php

1
2
3
4
abstract class StudentAbstract {
    abstract function getFirstName();
    abstract function getLastName();
}

FirstGradeStudent.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
include_once('StudentAbstract.php');

class FirstGradeStudent extends StudentAbstract {
    private $firstName;
    private $lastName;

    private function __construct() {
        $this->firstName = "Garry";
        $this->lastName = "Lachman";
    }

    public function getFirstName()  {
        return $this->firstName;
    }

    public function getLastName()   {
        return $this->lastName;
    }
}

SecondGradeStudent.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
include_once('StudentAbstract.php');

class SecondGradeStudent extends StudentAbstract    {
    private $firstName;
    private $lastName;

    private function __construct() {
        $this->firstName = "Polani";
        $this->lastName = "Almoni";
    }

    public function getFirstName()  {
        return $this->firstName;
    }

    public function getLastName()   {
        return $this->lastName;
    }
}

GetStudentFactory.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
include_once("FirstGradeStudent.php");
include_once("SecondGradeStudent.php");

class GetStudentFactory {
    public function getStudent($grade)  {
        $student = NULL;
        switch ($grade) {
            case "first":
                $student = new FirstGradeStudent();
            break;

            case "second";
                $student = new SecondGradeStudent();
            break;

            default:
                $student = new FirstGradeStudent();
            break;
        }
        return $student;
    }
}

Test.php

1
2
3
4
5
6
7
8
9
include_once("GetStudentFactory.php");

$factory = new GetStudentFactory();

$firstGrade = $factory->getStudent("first");
// return FirstGradeStudent class

$secondGrade = $factory->getStudent("second");
// return SecondGradeStudent class

Have Fun ;)
Garry Lachman

VN:F [1.9.0_1079]
Rating: 10.0/10 (1 vote cast)
VN:F [1.9.0_1079]
Rating: +1 (from 1 vote)
  • Share/Bookmark
12Apr/100

Doru Moisa’s Blog: Static call versus Singleton call in PHP

New blog post by Doru Moisa about Static call vs. Singleton... very recommended!!!

"In the past several months I’ve been working with a rather large application built with symfony. I noticed that symfony makes heavy use of the Singleton pattern (other frameworks, like Zend do that too)..."

http://moisadoru.wordpress.com/2010/03/02/static-call-versus-singleton-call-in-php/

Enjoy ;)
Garry Lachman

VN:F [1.9.0_1079]
Rating: 10.0/10 (1 vote cast)
VN:F [1.9.0_1079]
Rating: +1 (from 1 vote)
  • Share/Bookmark
12Apr/100

Adapter Design Pattern in PHP5

Adapter patter is very simply pattern that make an interface
for another class using his functions

ExampleItem.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<?php
class ExampleItem   {
    private $name;
    private $title;

    private fucntion __construct($name, $title) {
        $this->name = $name;
        $this->title = $title;
    }

    public function getName()   {
        return $this->name;
    }

    public function getTitle()  {
        return $this->title;
    }
}
?>

ExampleItemAdapter.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?php
include("ExampleItem.php");

class ExampleItemAdapter    {
    private $exampleItem;

    private fucntion __construct(ExampleItem $exampleItem)  {
        $this->exampleItem = $exampleItem;
    }

    public function getItemInfo()   {
        return $this->exampleItem->getName() . " - " . $this->exampleItem->getTitle();
    }
}
?>

testAdapter.php

1
2
3
4
5
6
7
8
9
<?php
include("ExampleItem.php");
include("ExampleItemAdapter.php");

$test = new ExampleItemAdapter(new ExampleItem("Garry Lachman","SomeTitle"));

echo $test->getItemInfo();
// result: "Garry Lachman - SomeTitle"
?>

Have Fun ;)
Garry Lachman

VN:F [1.9.0_1079]
Rating: 10.0/10 (1 vote cast)
VN:F [1.9.0_1079]
Rating: +1 (from 1 vote)
  • Share/Bookmark
8Apr/101

“ereg is deprecated” warnings in PHP 5.3

If you try to install systems like OSCommerce on PHP 5.3.x you may got strange
error like "ereg is deprecated...".

Here is the solution!!!

1
ereg('RegExp', $x $y);

becomes

1
preg_match('/RegExp/', $x $y);

dont forget "/ /" between the regexp code!!!

Same for "ereg_replace"

1
ereg_replace('RegExp', $x, $y);

becomes

1
preg_replace('/RegExp/', $x, $y);

Thanks,
Garry Lachman

VN:F [1.9.0_1079]
Rating: 10.0/10 (1 vote cast)
VN:F [1.9.0_1079]
Rating: +1 (from 1 vote)
  • Share/Bookmark
25Feb/103

Singleton Pattern in PHP5

Singleton is very basic design pattern.
If the instance is inited once than the class return the same instance.

Example of signleton in php5

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<?
Class SingletonExample {
  static private $instance;

  private function __construct() {
  }

  static function getInstance() {
    if(!Self::$instance) {
      Self::$instance = new SingletonExample();
    } else {
      return Self::$instance
    }
  }

  public function doHello() {
     return "Hello World";
   }
?>

Usage Example:

1
2
3
4
5
6
<?
include_once("singleton.class.php");

$single = SingletonExample::getInstance();
echo $single->doHello(); //return "Hello World"
?>
VN:F [1.9.0_1079]
Rating: 10.0/10 (1 vote cast)
VN:F [1.9.0_1079]
Rating: +1 (from 1 vote)
  • Share/Bookmark

Categories

Recent Posts

Tag Cloud

Recent Comments

Popular Posts

Meta

More

Web Development Blogs - BlogCatalog Blog Directory

Subscribe

make money make money online http://gfxdaily.com 3d photos tutorials Website design tools Premium WordPress theme 3.0.1 Internet marketing broken down step by step. Service billing invoices - invoices
Powered by: Reciprocal link exchange     Add Site