FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » Taking parameters into functions
Show: Today's Messages :: Polls :: Message Navigator
Switch to threaded view of this topic Create a new topic Submit Reply
Taking parameters into functions [message #175657] Sun, 16 October 2011 12:56 Go to next message
houghi is currently offline  houghi
Messages: 45
Registered: September 2011
Karma: 0
Member
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
Re: Taking parameters into functions [message #175658 is a reply to message #175657] Sun, 16 October 2011 13:27 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
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(at)attglobal(dot)net
==================
Re: Taking parameters into functions [message #175659 is a reply to message #175658] Sun, 16 October 2011 14:16 Go to previous messageGo to next message
houghi is currently offline  houghi
Messages: 45
Registered: September 2011
Karma: 0
Member
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" <
Re: Taking parameters into functions [message #175660 is a reply to message #175657] Sun, 16 October 2011 16:57 Go to previous messageGo to next message
Peter H. Coffin is currently offline  Peter H. Coffin
Messages: 245
Registered: September 2010
Karma: 0
Senior Member
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
Re: Taking parameters into functions [message #175661 is a reply to message #175660] Sun, 16 October 2011 18:15 Go to previous messageGo to next message
houghi is currently offline  houghi
Messages: 45
Registered: September 2011
Karma: 0
Member
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" <
Re: Taking parameters into functions [message #175662 is a reply to message #175659] Sun, 16 October 2011 18:32 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
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(at)attglobal(dot)net
==================
Re: Taking parameters into functions [message #175665 is a reply to message #175657] Mon, 17 October 2011 08:10 Go to previous message
alvaro.NOSPAMTHANX is currently offline  alvaro.NOSPAMTHANX
Messages: 277
Registered: September 2010
Karma: 0
Senior Member
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
--
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: Stats comp.lang.php (last 7 days)
Next Topic: Editing a combobox
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Thu Sep 19 22:09:27 GMT 2024

Total time taken to generate the page: 0.02618 seconds