Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.php > #3433 > unrolled thread

Taking parameters into functions

Started byhoughi <houghi@houghi.org.invalid>
First post2011-10-16 14:56 +0200
Last post2011-10-17 10:10 +0200
Articles 7 — 4 participants

Back to article view | Back to comp.lang.php


Contents

  Taking parameters into functions houghi <houghi@houghi.org.invalid> - 2011-10-16 14:56 +0200
    Re: Taking parameters into functions Jerry Stuckle <jstucklex@attglobal.net> - 2011-10-16 09:27 -0400
      Re: Taking parameters into functions houghi <houghi@houghi.org.invalid> - 2011-10-16 16:16 +0200
        Re: Taking parameters into functions Jerry Stuckle <jstucklex@attglobal.net> - 2011-10-16 14:32 -0400
    Re: Taking parameters into functions "Peter H. Coffin" <hellsop@ninehells.com> - 2011-10-16 11:57 -0500
      Re: Taking parameters into functions houghi <houghi@houghi.org.invalid> - 2011-10-16 20:15 +0200
    Re: Taking parameters into functions "Álvaro G. Vicario" <alvaro.NOSPAMTHANX@demogracia.com.invalid> - 2011-10-17 10:10 +0200

#3433 — Taking parameters into functions

Fromhoughi <houghi@houghi.org.invalid>
Date2011-10-16 14:56 +0200
SubjectTaking parameters into functions
Message-ID<slrnj9ll4o.3ru.houghi@penne.houghi>
I have the following code:

$File = "test.txt";
function write_fifo($data,$File) {
  echo $data."<br>".$File."<hr>";
  $fh = fopen($File, 'w') or exit("can't open file -".$File."= .");
  fwrite($fh, $data);
  fclose($fh);
}
write_fifo("Something Here",$File);

This works. What I want to ask is about the write_fifo() part. Is it
possible to have something like the following:
write_fifo("Something Here");
instead of the line including $File.

Not a real must. Just a nice to have. It would make my code perhaps a
bit cleaner AND I would have learned something that I could use in the
future.

I have looked, but could not find anything. Probably because I have no
idea what I should be looking for (php novice and all that)

houghi
-- 
This is written under the inluence of the following:
>   Artist : Evanescence
>     Song : Exodus
>    Album : EP

[toc] | [next] | [standalone]


#3434

FromJerry Stuckle <jstucklex@attglobal.net>
Date2011-10-16 09:27 -0400
Message-ID<j7em50$knr$1@dont-email.me>
In reply to#3433
On 10/16/2011 8:56 AM, houghi wrote:
> I have the following code:
>
> $File = "test.txt";
> function write_fifo($data,$File) {
>    echo $data."<br>".$File."<hr>";
>    $fh = fopen($File, 'w') or exit("can't open file -".$File."= .");
>    fwrite($fh, $data);
>    fclose($fh);
> }
> write_fifo("Something Here",$File);
>
> This works. What I want to ask is about the write_fifo() part. Is it
> possible to have something like the following:
> write_fifo("Something Here");
> instead of the line including $File.
>
> Not a real must. Just a nice to have. It would make my code perhaps a
> bit cleaner AND I would have learned something that I could use in the
> future.
>
> I have looked, but could not find anything. Probably because I have no
> idea what I should be looking for (php novice and all that)
>
> houghi

You could, by making $File global in your function.  But it's a very bad 
thing to do.  It now makes the function dependent on a variable outside 
of it.  You couldn't, for instance, use the function with a variable 
named $outfile.

Parameters are good things to pass to functions, and you should strive 
to use them as much as necessary, not get rid of the for "cleaner code". 
  Your code will end up much "dirtier" in the long run.

-- 
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

[toc] | [prev] | [next] | [standalone]


#3435

Fromhoughi <houghi@houghi.org.invalid>
Date2011-10-16 16:16 +0200
Message-ID<slrnj9lpq5.3ru.houghi@penne.houghi>
In reply to#3434
Jerry Stuckle wrote:
> Parameters are good things to pass to functions, and you should strive 
> to use them as much as necessary, not get rid of the for "cleaner code". 
>   Your code will end up much "dirtier" in the long run.

With cleaner I mean I won't have several (around 15 for now. This will
increase) lines where I might need to edit $File to $FILE or whatever if
I change some code. For sure I will forget at least one line somewhere.

I do not need it here. I could as easily use:
function write_fifo($data) {
  $File = "test.txt";
  echo $data."<br>".$File."<hr>";
  $fh = fopen($File, 'w') or exit("can't open file -".$File."= .");
  fwrite($fh, $data);
  fclose($fh);
}
write_fifo("Something Here");

Or write it in full and not use a parameter at all for $File.

write_fifo("Something Here"); is much more logical then
write_fifo("Something Here";"some_file"); as it involves an action,
rather then just writing to a file. The real name will not be
write_fifo, but rather mplayer

It will steer mplayer that is running in slave mode. e.g. one will be:
Some of them:
mplayer("pause");		// Pause/Play 
mplayer("quit");		// Quit
mplayer("volume -10");		// Louder
mplayer("volume +10");		// Softer
mplayer("loadfile $film 0");	// Start playing a movie

So the $File part is not something I would like to have there as it does
not make thing more clear, but instead will make it less clear what is
going on or what the code is supposed to do at that point.


houghi
-- 
>	  Beware of he who would deny you access to information,	 <
>	      for in his heart he dreams himself your master.		 <
>	   Commissioner Pravin Lal: "U.N. Declaration of Rights"	 <

[toc] | [prev] | [next] | [standalone]


#3439

FromJerry Stuckle <jstucklex@attglobal.net>
Date2011-10-16 14:32 -0400
Message-ID<j7f809$8dr$2@dont-email.me>
In reply to#3435
On 10/16/2011 10:16 AM, houghi wrote:
> Jerry Stuckle wrote:
>> Parameters are good things to pass to functions, and you should strive
>> to use them as much as necessary, not get rid of the for "cleaner code".
>>    Your code will end up much "dirtier" in the long run.
>
> With cleaner I mean I won't have several (around 15 for now. This will
> increase) lines where I might need to edit $File to $FILE or whatever if
> I change some code. For sure I will forget at least one line somewhere.
>
> I do not need it here. I could as easily use:
> function write_fifo($data) {
>    $File = "test.txt";
>    echo $data."<br>".$File."<hr>";
>    $fh = fopen($File, 'w') or exit("can't open file -".$File."= .");
>    fwrite($fh, $data);
>    fclose($fh);
> }
> write_fifo("Something Here");
>
> Or write it in full and not use a parameter at all for $File.
>
> write_fifo("Something Here"); is much more logical then
> write_fifo("Something Here";"some_file"); as it involves an action,
> rather then just writing to a file. The real name will not be
> write_fifo, but rather mplayer
>
> It will steer mplayer that is running in slave mode. e.g. one will be:
> Some of them:
> mplayer("pause");		// Pause/Play
> mplayer("quit");		// Quit
> mplayer("volume -10");		// Louder
> mplayer("volume +10");		// Softer
> mplayer("loadfile $film 0");	// Start playing a movie
>
> So the $File part is not something I would like to have there as it does
> not make thing more clear, but instead will make it less clear what is
> going on or what the code is supposed to do at that point.
>
>
> houghi

But what are you writing TO?  That's part of the action, also.  Or in 
the case or mplayer - which mplayer are you talking to?

In your example you have an action - and an object that action is 
applied to.  The object is just as important as the action.


-- 
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

[toc] | [prev] | [next] | [standalone]


#3437

From"Peter H. Coffin" <hellsop@ninehells.com>
Date2011-10-16 11:57 -0500
Message-ID<slrnj9m378.hbg.hellsop@nibelheim.ninehells.com>
In reply to#3433
On Sun, 16 Oct 2011 14:56:56 +0200, houghi wrote:
> I have the following code:
>
> $File = "test.txt";
> function write_fifo($data,$File) {
>   echo $data."<br>".$File."<hr>";
>   $fh = fopen($File, 'w') or exit("can't open file -".$File."= .");
>   fwrite($fh, $data);
>   fclose($fh);
> }
> write_fifo("Something Here",$File);
>
> This works. What I want to ask is about the write_fifo() part. Is it
> possible to have something like the following:
> write_fifo("Something Here");
> instead of the line including $File.
>
> Not a real must. Just a nice to have. It would make my code perhaps a
> bit cleaner AND I would have learned something that I could use in the
> future.
>
> I have looked, but could not find anything. Probably because I have no
> idea what I should be looking for (php novice and all that)

In addition to Jerry's method (which I agree is a bad idea, for the
reasons he gives), you can also default $File in the function
declaration and make the parameter optional

function write_fifo($data,$File = "test.txt") {
  echo $data."<br>".$File."<hr>";
  $fh = fopen($File, 'w') or exit("can't open file -".$File."= .");
  fwrite($fh, $data);
  fclose($fh);
}

You can even acheive a level of portability by defaulting the setting of
the parameter to a constant, like

define ("fifoFile", "/path/to/default/fifo/file");
...
function write_fifo($data,$File = fifoFile) {
  echo $data."<br>".$File."<hr>";
  $fh = fopen($File, 'w') or exit("can't open file -".$File."= .");
  fwrite($fh, $data);
  fclose($fh);
}

-- 
2. My ventilation ducts will be too small to crawl through.
        --Peter Anspach's list of things to do as an Evil Overlord

[toc] | [prev] | [next] | [standalone]


#3438

Fromhoughi <houghi@houghi.org.invalid>
Date2011-10-16 20:15 +0200
Message-ID<slrnj9m7pf.3o4.houghi@penne.houghi>
In reply to#3437
Peter H. Coffin wrote:
> In addition to Jerry's method (which I agree is a bad idea, for the
> reasons he gives), you can also default $File in the function
> declaration and make the parameter optional

OK, Ik wil, look for a different solution.

Still interested in a solution.

> function write_fifo($data,$File = "test.txt") {

Earier on my page I already named $File. It would be strange to have to
do this twice or more times. If I change the file I would rather do it
in only one place instead of 2 or more.

>
> You can even acheive a level of portability by defaulting the setting of
> the parameter to a constant, like
>
> define ("fifoFile", "/path/to/default/fifo/file");
> ...
> function write_fifo($data,$File = fifoFile) {

Now this is usefull and I will look into it. As far as I can see I would
only need to do a define once and it will be used all over the place.
That is exactly what I want.

houghi
-- 
>	  Beware of he who would deny you access to information,	 <
>	      for in his heart he dreams himself your master.		 <
>	   Commissioner Pravin Lal: "U.N. Declaration of Rights"	 <

[toc] | [prev] | [next] | [standalone]


#3442

From"Álvaro G. Vicario" <alvaro.NOSPAMTHANX@demogracia.com.invalid>
Date2011-10-17 10:10 +0200
Message-ID<j7gnsv$f0i$1@dont-email.me>
In reply to#3433
El 16/10/2011 14:56, houghi escribió/wrote:
> I have the following code:
>
> $File = "test.txt";
> function write_fifo($data,$File) {
>    echo $data."<br>".$File."<hr>";
>    $fh = fopen($File, 'w') or exit("can't open file -".$File."= .");
>    fwrite($fh, $data);
>    fclose($fh);
> }
> write_fifo("Something Here",$File);
>
> This works. What I want to ask is about the write_fifo() part. Is it
> possible to have something like the following:
> write_fifo("Something Here");
> instead of the line including $File.

That'd be pretty confusing. How about this?

$fw = new FifoWriter();
$fw->open('foo.txt');

$fw->write('Lorem ipsum dolor sit amet');
$fw->write('Consectetur adipisicing elit');

$fw->close();


The code would roughly look like this:

class FifoWriter{
	private $fp;

	public function open($file){
		$fp = fopen($file, 'w');
		if($fp){
			$this->fp = $fp;
		}else{
			throw new Exception('Could not open file: ' . $file);
		}
	}

	public function open($file){
		$fp = fopen($file, 'w');
		if($fp){
			$this->fp = $fp;
		}else{
			throw new Exception('Could not open file: ' . $file);
		}
	}
	
	public function write($data){
		if( !fwrite($this->fp, $data) ){
			throw new Exception('Could not write data');
		}
	}

	public function close($file){
		fclose($this->fp);
	}
}



-- 
-- http://alvaro.es - Álvaro G. Vicario - Burgos, Spain
-- Mi sitio sobre programación web: http://borrame.com
-- Mi web de humor satinado: http://www.demogracia.com
--

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.php


csiph-web