RSS

Monthly Archives: June 2009

Another PHP5 and C# OOP difference

A few days ago I have found out for myself about one more difference between PHP and C# OOP impelentations. Look at the following code snippet in PHP:

class ParentClass {
    public $val = 'Parent';
	
    public function GetValue()
    {
        echo $this->val;
    }
}

class ChildClass extends ParentClass {
     public $val = 'Child';
}

$a = new ChildClass();
$a->GetValue();

What do you expect it to show – ‘Parent’ or ‘Child’? The PHP manual says the result will be ‘Child’. And it seems quite logical to me as I would expect public property to be overridden.

Now rewrite the same code to C#:

class Parent   {
     public string val = "Parent";

     public void GetValue()  {
            Console.WriteLine(this.val);
     }
}

class Child:Parent  {
     public string val = "Child";
}

Child child = new Child();
child.getValue();

Run the code and you will get ‘Parent’…

Change access modifier of val to private in the parent class. Now both PHP and C# outputs the same – ‘Parent’. Why?

First thing to mention is that unlike PHP, public identifiers cannot be overridden in C#. Calling GetValue(), this points to parent object and returns “not overridden” value of val as a result. Private identifers inside parent are not accessible for the child and therefore are not overridden so everything becomes clear.

To make C# behavior similar to PHP you should either add a method with the same signature and with override keyword to Child class and mark parent method as virtual:

class Parent   {
     public string val = "Parent";

     public virtual void GetValue()  {
            Console.WriteLine(this.val);
     }
}

class Child:Parent  {
     public string val = "Child";

     public override void GetValue() {
         Console.WriteLine(this.val);
     }
}

or use overridden public properties to access the val value. This will work correctly as unlike public fields, public properties can be overridden.

class Parent {
    private string privateVal = "Parent";
    public virtual string val
    {
        get { return privateVal; }
    }

    public void GetValue()
    {
        Console.WriteLine(this.val);
    }
}

class Child : Parent {
    private string privateVal = "Child";
    public override string val
    {
        get { return privateVal; }
    }
}

I couln’t find any “standard” behaviour for this case. So I guess it was up to C# and PHP teams how to implement this.

 
2 Comments

Posted by on June 28, 2009 in ASP.NET, PHP

 

Tags: , ,

A note about DreamWeaver

Today my client wanted to update a text on the page with DreamWeaver… just a text and it ended up with the whole page screwed up…

There is no reason to blame DreamWeaver of course – it does its job.

My advise for customers – if it’s possible use CMS next time.

 
1 Comment

Posted by on June 27, 2009 in misc

 

Free PHP Helpdesk

I spent a few hours today searching for a good free PHP helpdesk system. Most of those that I found threw all sorts of php and javascript errors. I already started picturing the rest of my day fixing those errors.

Then bumped into Hesk. It turned out to be a quite nice and easy to install/use script. In case you are surfing Internet in vain (like I did) have a look at my working copy of Hesk here.

A few screenshots:

Hesk screenshot
Hesk screenshot

 
3 Comments

Posted by on June 27, 2009 in misc

 

Tags: , , ,

wix.com

For the last years people were thinking where WEB is going to and what it will be its nearest future. Seems like now it’s getting more and more obvious that with services like www.wix.com WEB will soon become really just a tool for both business and personal activity.

You may say that it’s flash and not so many people would prefer (as we can see all over the WEB now) it to HTML websites. We all know what advantages and disadvantages of flash are and I won’t repeat them all over again. But taking into account wide bandwidth connections and how easy WIX made it to use the tool my opinion is that it’s perfect for private and small business use. And plus Google analytics, unlimited bandwidth, backups and only for $12.42 per month you can have it all on your own domain name and with no ads.

Another thing I was thinking of is if it’s possible to combine FLASH and HTML version? Say you already have a HTML website with a domain name and interesting content, can WIX host this version too? If they do that would be brilliant as you will have to pay only for the hosting services and get a flash version of website for free! But this have to be confirmed.

 
Leave a comment

Posted by on June 26, 2009 in misc

 

Update multiple rows with different values

As far as I am aware there are about 3 distinct ways to update multiple rows in SQL Server database with different values.

Earlier I mentioned how to insert multiple rows in a single SQL statement using row constructors. Unfortunately there is no way to do something like this pseudo code bellow for UPDATEs:

UPDATE myTable (col1,col2) SET 
    (val1,val2) WHERE col3='val3',
    (val3,val4) WHERE col3='val3'  

or

UPDATE myTable SET 
    (col1=val1, col2=val2),
    (col1=val3,col2=val4)  
WHERE (col3='val1'),(col3='val4')

So what are the options? I picked out the following three. If you know any others please share! These methods can be used separately depending on the way you get the data for update, or can be combined inside a stored procedure.

  1. Execute multiple UPDATE statements

    This implies execution of a list of single UPDATE commands. Look at this example bellow for the main idea:

    com = com & "UPDATE myTable SET col1='val1', col2='val4' WHERE col3='val7';"
    com = com & "UPDATE myTable SET col1='val2', col2='val5' WHERE col3='val8';"
    com = com & "UPDATE myTable SET col1='val3', col2='val6' WHERE col3='val9';"
    
    Execute(com)
    
  2. Such trivial updates are quick and optimized. The data for update could be either a scalar data or taken from the subquery results. The only drawback of this approach is that each update is executed in its own separate transaction.

  3. Using CASE expression

    CASE expressions were introduced in SQL-92 and are available since SQL Server 2005. Read more on CASE expressions in the MSDN article.

    UPDATE myTable
    SET col1 = CASE col2 WHEN '1' THEN 'one' WHEN '2' THEN 'two' END
    

    To speed things up it might be a good idea to reduce the search criteria by adding WHERE … IN … clause:

    UPDATE myTable
    SET col1 = CASE col2 WHEN '1' THEN 'one' WHEN '2' THEN 'two' END
    WHERE col2 IN ('1','2',...)
    

    To update more than one field you have to specify the list of columns just like for an common UPDATE.

    UPDATE myTable SET 
    col1 = CASE col3 WHEN '1' THEN 'one' WHEN '2' THEN 'two' END,  
    col2 = CASE col4 WHEN '1' THEN 'one' WHEN '2' THEN 'two' END  
    WHERE col1 IN ('1','2')  
    

    Note, all data types used in the THEN clause of CASE expressions must be compatible data types. If the data type used is not compatible then SQL Server will return an error because implicit data type conversion is not supported.

    According to Execution plan query with CASE expression takes about the same time as trivial updates as it doesn’t require a full table scan and uses clustered index to locate the record.

  4. Update one table with JOIN on another table

    Using JOIN you can update one table based on the contents of another table or tables. Great news as starting SQL Server 2008 it’s possible to use new parameter type – table-valued parameters. This means that you can send multiple rows of data to a Transact-SQL statement or a routine without creating a temporary table or many parameters!

    In the following example I declare in-memory data table bufTable, insert a few rows into it and update myTable with values taken from bufTable:

    DECLARE @bufTable TABLE(id INT, col1 VARCHAR(30));
    
    INSERT INTO @bufTable(id,col1)(
       SELECT 1, '11' UNION ALL
       SELECT 2, '22' UNION ALL
       SELECT 3, '33');
    
    UPDATE myTable set myTable.col1 = @bufTable.col1  FROM myTable  
    INNER JOIN @bufTable on myTable.id = @bufTable.id and @bufTable.id='5'  
    

    Still you don’t have to use table variables; you may want to create a temporary table or use an already existing regular table.

    Now I don’t take into consideration the resources for temporary table or table variable to be created. According to the Execution plan this method required more I/O recourses comparing to other methods as the data has to be retrieved and joined with another table. The update action is made to the results of that join.

    Note, unlike for temporary tables, statistics cannot be created against table variables. This shouldn’t be a problem though as table variables generally exist for a specific purpose and aren’t used for a wide range of ad-hoc queries.

As you see there shouldn’t be any problem to choose between the 3 methods – the best solution strongly depends on the source and format you have the data in and the results you are trying to achieve.

And the last thing – once you have chosen the appropriate option, you may want to run these methods or inside a stored procedure. And you will face another problem – SQL Server doesn’t support arrays as parameters to stored procedures.

Again you have three options 🙂

  1. Execute stored procedure in a loop with new parameters each time
  2. Declare a varchar(4000) or text variable that would contain your comma separated data. Then you would have to split that list in the stored procedure. Alternative you can pass data as an XML string parameter and use OPENXML rowset provider inside of the stored procedure to select values from the provided XML. Read more about this solution in this article. Erland Sommarskog wrote about the performance of the different methods here – http://www.sommarskog.se/arrays-in-sql-perftest.html.
  3. If you’re using SQL Server 2008, you can enjoy the possibility to use new table-valued parameters. Table-valued parameters allow you to pass a whole table of data into a stored procedure in one go. Rather than passing XML or a string, multi-valued parameters can be represented quite naturally.
 
13 Comments

Posted by on June 18, 2009 in SQL

 

Tags: , , , ,

Unicode parameters for stored procedures

When working on your ASP + SQL Server application you might encounter Unicode data is shown as ‘??????’. At least I did when started rewriting an old code to use Stored Procedures.

The ‘N’ prefix works great in both cases – when you execute stored procedure:

EXEC saveNames N'Даша', N'Сало'

or run a single query:

INSERT INTO Persons (firstName, surName) VALUES (N'Даша', N'Сало')

Note, we should use ‘N’ prefix on a Unicode string because otherwise SQL Server will convert it to the non-Unicode code page of the database. See the link for more details.

This common solution doesn’t work for stored procedure parameters. Look at the code bellow – if you try and add ‘N’ prefix there, you will get an error.

cmd.Parameters.Append cmd.CreateParameter ("@parsonName",adVarChar,adParamInput ,50,"Даша Сало")

What you have to do to make it work is:

  • Set the type of created parameters to adVarWChar;
  • Const adVarWChar = 202
    
    cmd.Parameters.Append cmd.CreateParameter ("@parsonName", adVarWChar,adParamInput, 50, "Даша Сало")
    
  • Declare stored procedure input parameters as nvarchar;
  • ALTER PROCEDURE SaveName (@firstName nvarchar(50), @surname nvarchar(50))
    
  • If you declare any variables inside stored procedure that works with Unicode data, these variables must have nvarchar type;
  • DECLARE @buf NVARCHAR(50)
    
  • You may also have to save ASP page in UTF-8 with Signature encoding. It depends on the way you receive Unicode data.

There is nothing new I just wanted to have this information in one place for anybody who gets the same problem.

 
1 Comment

Posted by on June 11, 2009 in misc

 

Perch – a tiny CMS

Perch is new really small CMS created by edgeofmyseat.com. It perfectly suites simple static websites that require some text changes to be made from time to time. It costs only £35 as a one-off unrestricted license cost per domain and doesn’t require any additional monthly payments.

It’s easy to install and customize on shared hosting. All you need is PHP5 and MySQL4.1 or later.

The great thing about Perch is that it does exactly what you expect of it without any unnecessary heavy functionality.

Unfortunately they don’t provide a trial version yet but you can download a Compatibility Test Suite to be sure it’s going to work on your hosting. Very nice of them!

I recommend to have a look at their website for detailed instructions, which are nicely written by the way.

 
Leave a comment

Posted by on June 1, 2009 in misc

 

Tags: ,

 
%d bloggers like this: