Programming



.NET Style Properties (Getters/Setters) in PHP


DSC01202 On any given day, I can be programming in one of many languages (C# .NET, PHP, JavaScript, CSS, & HTML [I know I may be pushing it calling CSS & HTML languages], and soon Visual Basic .NET because of a collaborative project).  Don’t get me wrong, I love that part of my job.  It keeps me sharp.  Sure there is the occasional mishap where I forget that the . is the member accessor in the .NET languages, but it is the concatenation operator in PHP and I will throw the occasional $ into my C# code…

Another thing to know is that I am an object oriented programmer to the core…I am currently cleaning up after another developer who was not an object oriented guy…grr…there is data access code everywhere and he didn’t know the definition of object…finally I just took the functionality down because it was so bug ridden that whenever I would fix one bug, it would expose another.  One thing that has constantly bugged me since I started coding in PHP that I didn’t like its method of using __get and __set for getters and setters (mainly because I still use a modified version of Hungarian notation, but that is a discussion for another blog post, so I didn’t like what the getters and setters would look like).

Then, tonight, as I was coding, I had an epiphany, I could use PHP’s optional operator syntax for a function to get something close to what I would want.  So, where I would have coded this before:

   1:  <?php
   2:   
   3:  class Test {
   4:   
   5:  private $m_intId;
   6:   
   7:  public function setId($value) {
   8:   
   9:  $this->m_intId = $value;
  10:   
  11:  }
  12:   
  13:  public function getId() {
  14:   
  15:  return $this->m_intId();
  16:   
  17:  }
  18:   
  19:  }
  20:   
  21:  ?>

where I would do the following to access it $objTest->setId(1); or $intId = $objTest->getId(); (I would have to explicitly call the getter or setter)

Tonight I realized, I could code it this way:

   1:  <?php
   2:   
   3:  class Test {
   4:   
   5:  private $m_intId;
   6:   
   7:  public function Id($value = 0) {
   8:   
   9:  if($value != 0) {
  10:   
  11:  $this->m_intId = $value;
  12:   
  13:  } else {
  14:   
  15:  return $this->m_intId;
  16:   
  17:  }
  18:   
  19:  }
  20:   
  21:  ?>

Now, I can access the id by doing the following: $objTest->Id(5); or $intId = $objTest->Id();

What are your thoughts developers?

Share

.NET: One line could save your 64-bit bacon!


I have a customer that recently had to purchase a new computer due to her previous computer finally biting the dust.  Knowing what programs she was running on her computer, I advised her that she should purchase a 32-bit version of Vista (then upgrade to the 32-bit version of Windows 7 when it came out).

She bought a 32-bit machine that was running Vista and everything seemed fine except the OS was corrupt.  So she took the computer back in to have it replaced and they gave her a shiny new Windows 7 machine (she was only days away from the release of Windows 7) .  She asked them to give her the same computer, but they gave her a 64-bit version of Windows 7 so when she went to install her programs she ran into a ton of problems because the programs were not written for the 64-bit versions of the operating system (a lot of legacy applications are not written for 64-bit operating systems, this isn’t Microsoft’s fault, this is a fault of the application programmers).

I have a small application that I wrote that for ease of use and not having to install any other programs, I chose to use Microsoft Access as the database for the application (say what you will, it was a side project that turned into something I could sell), but there is no 64-bit JET driver for reading Access databases so for 64-bit operating systems, I was hosed, until I found out that one line could change everything.

In the csproj file of your C# .NET application, in the first property group, add this line:

<PlatformTarget>x86</PlatformTarget>

So your property group should look something like this:

<PropertyGroup>
<PlatformTarget>x86</PlatformTarget>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProductVersion>8.0.50727</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{A16625F1-A4BA-46FA-BD05-737257A18EBD}</ProjectGuid>
<OutputType>WinExe</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>UPurMEMORY</RootNamespace>
<AssemblyName>UPurMEMORY</AssemblyName>
<ApplicationIcon>UPurMEMORY.48.ico</ApplicationIcon>
<FileUpgradeFlags></FileUpgradeFlags>
<UpgradeBackupLocation></UpgradeBackupLocation>
<OldToolsVersion>2.0</OldToolsVersion>
</PropertyGroup>

That one little line saved my bacon and allows me to sell that piece of software for $10 to anyone who runs a 64-bit operating system.  I’m not saying this will solve all of your problems, but it helped me.

Related Links:

UPurMEMORY – memorization utility ($10.00 USD)
Windows 7 Home Premium (upgrade)
Windows 7 Professional (upgrade)
Windows 7 Ultimate (upgrade)

Related Posts:

What’s the big deal about Windows 7?

Share

PHP Coding: chmod() my new best friend


phpFor all of you coders out there, I just found a new best friend, its name is chmod().  For those of you familiar with Unix/Linux this command will look familiar.

Background: I was implementing an online store for a customer who already had an existing website, but wanted to be able to sell her goods online (I’m trying to keep things vague on purpose when describing the website).  I built her a custom content management system for her online store that allowed her to upload pictures of her goods and input pricing data.  My code would take the image she uploaded, save it, then copy it and resize the copy to make a thumbnail image.  For whatever reason though, her web host was locking down permissions on the first file.  This became problematic because it doesn’t do any good to have a file that you can’t read…

Solution: After using the move_uploaded_file() command I used the chmod() command to reset the file permissions.

Here is the format of the chmod() function: chmod($filename$mode );

I also ran into a gotcha that I didn’t expect…when specifying the mode, do not specify it as a string, (ie. “755”), instead you specify it as a number (ie. 755), even further though, you have to throw in a leading zero (ie. 0755).

Hopefully, you found this post helpful, happy coding.

Share