whitepj Posted May 28, 2009 Share Posted May 28, 2009 Hi all. Apologies. This is hopefully a stupid and easily answered Q. However, I'm lost, and need a bit of guidance... I have pecl-ps installed. The following code woks fine, and produces a very simple PS page: <?php $doc = PS_new(); if (!PS_open_file($doc, '-')) { die; } PS_set_parameter($doc, 'warning', 'true'); PS_set_info($doc, 'boundingbox', '0 0 596 842'); PS_set_info($doc,'Title', 'A Hello World Example'); PS_set_parameter($doc, 'SearchPath', '/usr/share/fonts/default/ghostscript'); PS_begin_page($doc, 596, 842); $font = PS_findfont($doc,'n021023l', '', 0); PS_setfont($doc, $font, 58.0); PS_set_value($doc,'charspacing', 2); PS_set_text_pos($doc, 10,10); PS_show_xy($doc, 'Hello World', 50,50); PS_setlinewidth($doc, 2); PS_moveto($doc, 50, 50); PS_lineto($doc, 100,100); PS_stroke($doc); ps_end_page($doc); ?> Now, what I want to do is get most of this into a class. Thus, my code would become: <?php class PS { var $ps; public function __construct($orientation='P', $unit='mm', $format='A4', $unicode=true, $encoding='UTF-8', $diskcache=false){ $this->ps = PS_new(); if (!PS_open_file($this->ps, '-')) { die; } // Construct PS file as above... } public function Output($name='doc.ps', $dest='I') { echo $this->getBuffer(); } protected function getBuffer() { return ps_get_buffer($this->ps); } } $doc = new PS('P', 'mm', 'A4', true, 'UTF-8', false); // do stuff $doc->Output('example.ps', 'I'); ?> Which again, works OK. However, obviously, what I want is the actual document writing code in a mass of other functions, and this is where I am failing - miserably! Any attempt to shift the 'construct PS' code elsewhere results in an error: <?php require_once('above-class.inc'); $ps = new PS('P', 'mm', 'A5', true, 'UTF-8', false); PS_begin_page($ps->ps, 596, 842); // or, perhaps $ps->WriteText($ps->ps, 'Hello World', 100, 100); ... more of the same ... $ps->Output('example_010.ps', 'I'); ?> Warning: ps_get_buffer(): 3 is not a valid ps document resource ... As you can probably tell, this is my first attempt at building a class, so a pointer on where I am going wrong, and how I can fix it, would be very much appreciated. Many thanks. Phil. Quote Link to comment Share on other sites More sharing options...
gizmola Posted May 28, 2009 Share Posted May 28, 2009 Probably this is because you are destroying the resource variable by passing it as a parameter by value. Resource variables are special variables that can't be copied or serialized. You didn't provide enough code to show us where the problem is because we don't have your WriteText() method. In terms of design however, it really doesn't make sense to have this line: $ps->WriteText($ps->ps, 'Hello World', 100, 100); While that's probably where you're having issues, it also doesn't make sense for you to pass a member variable ( ->ps) to a method of the same object, when you can refer to that variable using $this->, which you've done elsewhere in your code. That code should be something like: $ps->WriteText('Hello World', 100, 100); And inside the WriteText() method you would have: function WriteText($str, $x, $y) { PS_some_func($this->ps, $str, $x, $y etc....) Quote Link to comment Share on other sites More sharing options...
dbo Posted May 28, 2009 Share Posted May 28, 2009 I'd also suggest using PHP5 syntax for classes and instead of using var $ps and use private $ps instead. Quote Link to comment Share on other sites More sharing options...
whitepj Posted May 28, 2009 Author Share Posted May 28, 2009 Probably this is because you are destroying the resource variable by passing it as a parameter by value. Resource variables are special variables that can't be copied or serialized. OK. This is obviously the crux of the matter. When I call PS_new(), it returns a pointer to the postscript document (http://pslib.sourceforge.net/documentation.php?manpage=PS_new). This pointer is then used in all subsequent 'document write' actions. If at all possible, I am looking to use this pointer anywhere in my code, not just inside the class code. You didn't provide enough code to show us where the problem is because we don't have your WriteText() method. Yes, I did Effectively, it condenses down to: echo PS_get_buffer($this->ps); Where $this->ps is the aforementioned document pointer. (http://pslib.sourceforge.net/documentation.php?manpage=PS_get_buffer) In terms of design however, it really doesn't make sense to have this line: $ps->WriteText($ps->ps, 'Hello World', 100, 100); Of course it doesn't - it is complete crap. I apologise - I'm not sure what I was thinking at the time... What I meant (to illustrate the idea of using the pointer outside the class, was: PS_lineto($this->ps, 100,100); Am I explaining myself OK? Thanks. Quote Link to comment Share on other sites More sharing options...
gizmola Posted May 30, 2009 Share Posted May 30, 2009 Ok, so, just to nitpick, php doesn't have pointers. It has various variable types, and the one you are talking about is called a "resource". When I'm not getting is why you want to use a class, if all you're not going to encapsulate the native methods, and all you want to do is store the handle and then get it back out of the object again. The technical answer to why your example writetext didn't work is probably that, as I stated, you were passing the resource variable by value. In the process a copy is made and this is probably squashing the resource. Try modifying that function call so that the parameter passes by reference, although again, an instance variable internal to the class doesn't need to be passed at all -- it's already available from $this->. If you want to write a getPS method, then return &this->ps; HTH Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.