Jump to content

Recommended Posts

Hi everyone,

 

I'm converting some scripts I've written in Perl to PHP, and I have a couple of small packages in Perl that I need to convert to PHP to put the functionality on my website.  I'm not as experienced as I should be with  OO in both of the languages, so I have a few questions to help me bridge the gap:

 

PHP doesn't appear to have an equivalent to package.  How should I organize my PHP trnslation; should I make the entire base a class and have the perl classes internally defined?  i.e.

 

class PackageName {

  // all global constants should be put here, perhaps?

  class PerlObject1 {
    // stuff
  }

  class PerlObject2 {
    // stuff
  }

// etc....

}

 

 

Also, the Perl classes take high advantage of being able to dynamically define object-level variables from within the member functions.  Is the best way to implement this in PHP to have one data array keyed by the values, and insert them as necessary? 

 

if you can think of any other caveats that I should look out for in addition to these, please let me know.  Any help is apprecated.  Thanks!

 

Link to comment
https://forums.phpfreaks.com/topic/83904-converting-perl-packages-to-php/
Share on other sites

PHP doesn't appear to have an equivalent to package.  How should I organize my PHP trnslation; should I make the entire base a class and have the perl classes internally defined?

Unfortunately, PHP doesn't support embedded classes as pointed out here - though I still don't understand what that person meant exactly by 'type-parametric'...

The only way I can think of is just altering your class name, as ugly as it may seem.

 

Also, the Perl classes take high advantage of being able to dynamically define object-level variables from within the member functions.

PHP doesn't keep a strict watch over object variables. You can define new ones without grief from the engine. The only time it might throw a notice is when you try and read a variable that doesn't exist.

As an example, you can start with an stdClass (a bare object) and just throw in random variables, and it'll work.

$obj=new stdClass();
$obj->name="Bob";
$obj->age=52;
print_r($obj);

Should print out: "stdClass ( [name] => Bob, [age] => 52 )" or something like that.

As for method/member functions, it's the same concept:

class Test {
function stuff() {
  $this->name="Bob";
  $this->age=52;
  print_r($this); //Same as above, different class name
}
}
$obj=new Test();
$obj->stuff();

 

Is the best way to implement this in PHP to have one data array keyed by the values, and insert them as necessary?

I don't really get what you're asking here, since that's, well, already implented.

You can use variable variables, though:

$obj=new stdClass();
$var=array('name'=>'Bob','age'=>'52');
foreach ($var as $key => $value) {
$obj->$var=$value; //See variable variables in use here, with a sigil ($) being used on the variable after the member access operator (->)
}
print_r($obj); //Once again, same thing
//This actually has the same affect as (object)$var

 

if you can think of any other caveats that I should look out for in addition to these, please let me know.

I don't much about Perl, but I think that it is a little more lenient with its class structure than PHP. I've heard/read that you're able to manipulate your class structures (such as methods, inheritance, etc) run-time in Perl. You can't exactly do that in PHP. Dynamically defining members and using eval() is about as close as PHP gets to that.

 

Hope I could help with that.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.