Jump to content


Photo

PHP CLASS SYNTAX


  • This topic is locked This topic is locked
6 replies to this topic

#1 dsartain

dsartain
  • Members
  • PipPipPip
  • Advanced Member
  • 68 posts

Posted 18 March 2006 - 05:28 PM

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

#2 keeB

keeB
  • Staff Alumni
  • Advanced Member
  • 1,078 posts
  • LocationCalifornia

Posted 18 March 2006 - 06:32 PM

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.


class upload {

  private $uploadDir;

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

}

Trust me, it will save you lots of headache

Come visit my site to see my latest projects
http://nick.stinemates.org/wordpress/


#3 dsartain

dsartain
  • Members
  • PipPipPip
  • Advanced Member
  • 68 posts

Posted 18 March 2006 - 08:02 PM

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

class classname
{
private $variable;

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

}//end class


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

#4 hitman6003

hitman6003
  • Members
  • PipPipPip
  • Advanced Member
  • 1,807 posts

Posted 18 March 2006 - 08:34 PM

What he means is instead of using:

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

do:

function setuploaddir($uploaddir)
{
$this->uploaddir=$uploaddir;
}

Then call it by doing:

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


#5 keeB

keeB
  • Staff Alumni
  • Advanced Member
  • 1,078 posts
  • LocationCalifornia

Posted 19 March 2006 - 01:35 AM

Thanks for articulating that one better, hitman

Come visit my site to see my latest projects
http://nick.stinemates.org/wordpress/


#6 dsartain

dsartain
  • Members
  • PipPipPip
  • Advanced Member
  • 68 posts

Posted 19 March 2006 - 03:01 AM

[!--quoteo(post=356305:date=Mar 18 2006, 09:35 PM:name=keeB)--][div class=\'quotetop\']QUOTE(keeB @ Mar 18 2006, 09:35 PM) View Post[/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???

#7 keeB

keeB
  • Staff Alumni
  • Advanced Member
  • 1,078 posts
  • LocationCalifornia

Posted 20 March 2006 - 06:12 AM

[!--quoteo(post=356319:date=Mar 19 2006, 03:01 AM:name=Don)--][div class=\'quotetop\']QUOTE(Don @ Mar 19 2006, 03:01 AM) View Post[/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.

Come visit my site to see my latest projects
http://nick.stinemates.org/wordpress/





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users