Jump to content

new to PHP and some advice needed


andychurchill

Recommended Posts

Hi All,

 

I'm new to PHP other than doing basic things like form mailing scripts and so on, and I've just begun to use it for a more indepth project.  Coming from a Visual Studio/ASP.NET/c# background, the way of doing things in PHP has surprised me a little (although really it's not much different to classic ASP programming, I've just forgotten it all!).  I realise that the flexibility of PHP is its strength, even if it feels like a weakness to me right now :)

 

I've started by installing Netbeans IDE, along with PHP_Codesniffer, and I've been doing a lot of reading before I get dirty with code, (though I would love to find a Netbeans tool that allowed me to right click on a function and it try to guess what the function did based on the name of it, similar to ghostdocs in VisualStudio).

 

I'm obviously spoilt by Visual Studio's intellisense, and the heavily OO approach to development in that environment, but now that I've started coding in PHP it's a bit of an eye opener.  My natural way of developing would involve Enums, strongly typed clases/collections, etc.  Even with Netbeans IDE, the lack of type hinting on function return values, for example, and a number of other things, makes me a nervous :) I just wonder if there's a trade off with creating strongly typed classes, collections and enums in PHP, in terms of processing, just to get better code structure?

 

Collections:

 

For example, I've seen these examples for collection classes:

 

http://eide.org/2008/07/21/splobjectstorage/

http://codeutopia.net/blog/2008/09/17/generic-collections-in-php/

http://www.devshed.com/c/a/PHP/Collections-and-Sorting/

http://webscripts.softpedia.com/script/PHP-Clases/PHP-Collection-Class-11421.html

 

Does anyone here have any thoughts on this kind of thing? I find that comments on these blogs tend to be a bit dismissive of each others efforts rather than having an unbiased discussion on the best way to do something.

 

Constructors:

 

Then I started reading about constructors, and how you can't create multiple constructors as easily as you could in my usual environment, using these 2 articles as research:

 

http://ordinarywebguy.wordpress.com/2008/01/31/multiple-constructor-in-php/

http://www.alfonsojimenez.com/computers/multiple-constructors-in-php

 

So for example, if I wanted to be able to create a new object from a SOAP Response or as an item retrieved from a collection, I would effectively want to have two constructs like this:

 

public function __construct(SomeObject $result)

public function __construct($resultFromSOAPResponse)

 

which isn't possible as such, but can be recreated, sort of, using static methods? Again, what do people here suggest?

 

Enums:

 

Then I started reading about Enums, using the following examples:

 

http://it.toolbox.com/blogs/macsploitation/enums-in-php-a-native-implementation-25228

http://stackoverflow.com/questions/254514/php-and-enums

http://forums.devarticles.com/php-development-48/php-and-enumerations-8.html

 

and so on.  It's enough to make me want to run back to C#, but I thought I'd ask here first and find out what people think about these "issues".  I now wonder if it's worth even bothering trying to "recreate" what I'm used to in a PHP environment, and just stick to simpler coding to keep to PHP's strengths?

 

I just want to  get my hands dirty with some code now, but I want to ensure I start off on the right foot with my coding efforts, so that my PHP coding doesn't go the same way my old classic ASP coding used to go, with horrible undocumented unreadable spaghetti code, with no real structure.

Link to comment
Share on other sites

there's barely any good SOAP documentation. lots on NuSOAP however. I wrote a SOAP server using the standard SOAP extension, and can help if needed. If you have any specific ques let me know.

 

Well I've managed to get a nusoap client working with a 3rd party service, but I want to turn the results I received into a proper class structure.  That's the bit I'm struggling with - deciding how best to create those structures to replicate the results coming back, e.g. one one class (SomeObject) I have a property (StuffCollection) which is a collection of another class (Stuff).

 

So for example, that StuffCollection constructor could be called via the SomeObject's constructor, with results coming from the original soap response:

 

$newObject = new SomeObject($resultFROMSoapResponse)

 

so if I then did $newObject->getStuff(), this would be a collection, but the IDE wouldn't know this because it doesn't have type hinting on return values? but in theory, because I know what I'm being returned, I could do:

 

$newStuffCollection = new StuffCollection($newObject->getStuff())

 

That way my IDE now knows that newStuffCollection is a collection of Stuff, and would give me the correct properties/methods on this variable?

 

Similarly:

 

$newStuff = new Stuff($resultFromOriginalSOAPRequest); // used in the construct of StuffCollection when built from soap request

 

whereas I might later want to do:

 

$newStuff = new Stuff($newStuffCollection->getItem(0));

 

to ensure that the result of getItem is treated as a Stuff object, because I know that's what will be returned by getItem. 

 

This is what sparked this thread on whether it was worth me bothering to go to all the trouble of doing that, just to get a better IDE experience, when I should perhaps KIS(S), and not bother, which would be less processing overhead.

 

As I say, this doubt stems from my background in VS/ASP.NET/C# where you get treated very kindly, and are able to create strongly typed collections with excellent intellisense, so perhaps I just want too much from PHP.  As I say, this isn't necessarily a weakness of PHP, just a "new" way of thinking from a development point of view, which I'll get used to if need be, but thought I'd ask some experts first before I start on the project. :)

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.