Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Mar 2011
    Posts
    25
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Help translating Javascript code

    Good evening

    I'm trying to translate a Javascript into PHP. Most of the functions, procedures, etc ar not a problem to me (happly). But there's a structure I'm not sure I understand. Supose this piece of code in Javascript:

    Code:
    $ns.delta = {};
    
    $ns.delta.m_s = [25428, 23700, 22000, 21000, 19040, 17190, 15530, 14080, 12790, 11640];
    
    $ns.delta.demo = 0;
    $ns.delta.TABSTART = 1620;
    
    $ns.delta.calc = function (date) {
    	var p, B;
    	var diff = [0, 0, 0, 0, 0, 0];
    	var i, iy, k;
           (...etc...)
    }
    Is delta, in this case a class tranlated to PHP? Something like:

    Code:
    class delta
    {
        const m_s = array(25428, 23700, 22000, 21000, 19040, 17190, 15530, 14080, 12790, 11640);
        const demo = 0;
        const TABSTART = 1620;
    
        function calc($date) {
            diff = array(0, 0, 0, 0, 0, 0);
            (...etc...)
        }
    }
    and I would call the function like:

    Code:
    $result = delta::calc($date);
    or

    Code:
    $class = new delta();
    $result = $class->calc($date);
    ?

    I would apreciate the help.

    Kind regards,

    Kepler

  2. #2
    Master Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    5,700
    Thanks
    26
    Thanked 584 Times in 577 Posts
    Is delta, in this case a class tranlated to PHP?
    Since PHP--unlike JS--is class-based, that would work. Althougt it depends if that JS code is self-consistent (only then it would make sense to convert it to a class).

    Whether that should become a static (should be avoided as much as possible) or a non-static class depends on its use.
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  3. #3
    New Coder
    Join Date
    Mar 2011
    Posts
    25
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Hi,

    Thanks


 

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •