Jump to content

Archived

This topic is now archived and is closed to further replies.

dsartain

PHP CLASS SYNTAX

Recommended Posts

Hi guys, I'm relatively new to the PHP world, but I've taken several C++ courses so the syntax isn't that difficult. One of my friends who's helping me learn started griping because I did inline declarations (common in C++ from what I've been taught)...


//variable declaration
private $uploaddir;

//INLINE DECLARATION:
function setuploaddir(){$this->uploaddir=$_POST['upload_dir'];}

//STANDARD DECLARATION:
function setuploaddir()
{
$this->uploaddir=$_POST['upload_dir'];
}

Ok, to me it seems that since this is only doing one thing, inline should be fine and saves space, but I need to know what the experts think. How should I code this, or does it even matter...not when it comes to functionality, but as far as PHP standards go...? Thanks

Share this post


Link to post
Share on other sites
Hey Don --

You should code this pretty much how you see fit. If you like inline functions and you can read them really well, then it works for you and you should keep it that way. :D PHP is all about personal style. There's no 'right' way to do anything, and you will understand shortly, theres a LOT of ways to do the same thing. So let loose, enjoy yourself.. code it up the way you want it to be!

Just a quick thought though.. I wouldnt be using $_POST or $_SESSION variables inside of your class. I would initialize them outside of your class and use member variables and/or methods to handle the data.


[code]class upload {

  private $uploadDir;

  function setUploadDir($dir) { $this->uploadDir = $dir; }

}[/code]

Trust me, it will save you lots of headache

Share this post


Link to post
Share on other sites
when you say don't initialize them in my class, but do so elsewhere...what do you mean? The only way I've ever learned classes in C++ or PHP is

[code]
class classname
{
private $variable;

funciton setvariable($newvar){$this->variable=$newvar;}
function getvariable(){return $this->variable;}

}//end class

[/code]

granted the syntax is different in C++....but that's about it...how else should I initialize the variable???

Share this post


Link to post
Share on other sites
What he means is instead of using:

[code]function setuploaddir()
{
$this->uploaddir=$_POST['upload_dir'];
}[/code]

do:

[code]function setuploaddir($uploaddir)
{
$this->uploaddir=$uploaddir;
}[/code]

Then call it by doing:

[code]$classinstance->setuploaddir($_POST['upload_dir']);[/code]

Share this post


Link to post
Share on other sites
Thanks for articulating that one better, hitman

Share this post


Link to post
Share on other sites
[!--quoteo(post=356305:date=Mar 18 2006, 09:35 PM:name=keeB)--][div class=\'quotetop\']QUOTE(keeB @ Mar 18 2006, 09:35 PM) [snapback]356305[/snapback][/div][div class=\'quotemain\'][!--quotec--]
Thanks for articulating that one better, hitman
[/quote]

Oh, ok....I had things set up right in my code on the page...just didn't type it right on here...woops..any thought on inline verses the other way???

Share this post


Link to post
Share on other sites
[!--quoteo(post=356319:date=Mar 19 2006, 03:01 AM:name=Don)--][div class=\'quotetop\']QUOTE(Don @ Mar 19 2006, 03:01 AM) [snapback]356319[/snapback][/div][div class=\'quotemain\'][!--quotec--]
Oh, ok....I had things set up right in my code on the page...just didn't type it right on here...woops..any thought on inline verses the other way???
[/quote]

Personally, it's a matter a combination of mood, surrounding code, and amount of arguments, really.. I use inline style sparingly, some people are strict and want to conserve space. it's all about what you're interested in. good luck.

Share this post


Link to post
Share on other sites
Guest
This topic is now closed to further replies.

×

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.