Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.php > #3442
| From | "Álvaro G. Vicario" <alvaro.NOSPAMTHANX@demogracia.com.invalid> |
|---|---|
| Newsgroups | comp.lang.php |
| Subject | Re: Taking parameters into functions |
| Date | 2011-10-17 10:10 +0200 |
| Organization | http://alvaro.es/ |
| Message-ID | <j7gnsv$f0i$1@dont-email.me> (permalink) |
| References | <slrnj9ll4o.3ru.houghi@penne.houghi> |
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
--
Back to comp.lang.php | Previous | Next — Previous in thread | Find similar | Unroll thread
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
csiph-web