struct (turbo c ) = class (php) ?

  • 13 years ago
    hi,
      im obviously a newbie in PHP.. ^_^ .. just want to know how can i write this turbo C code into PHP. i mean, i want to know its equavalent code.

    Turbo C code that need to be change into PHP:
     struct
     {
       char Lname[30];
       char Fname[30];
       char address[30];
       char phone[30];
    } record[31];

    what i want to do here is to make my variables organized like this in Turbo c code:

    student[0].Lname = "john";
    student[0].Fname = "de la cruz";
    student[0].address = " somewhere out there";
    student[0].phone = "31234324342";

    student[1].Lname = "peter";
    student[1].Fname = "hill";
    student[1].address = " somewhere out there";
    student[1].phone = "12344412312";

    and so forth..

    thanks..

















      













  • 13 years ago
    The main difference between classes and structures is that in a class variables are private by default and in a structure they are public by default.

    If you are using php 5 you will want something like this to get similar functionality to a structure in c.:

    class someName{
        public $Lname;
        public $Fname;
        public $address;
        public $phone;

        public someName(){
           //class constructor
        }
    }










    To initialise the class you would do this

    $variableName=new someName();
    $variableName->Lname="John";
    echo ($variableName->Lname);



    However, it would be better practice to try and use get_ and set_ methods to change the objects state, rather than public instance variables. This is mainly because you can add validation and other stuff within the class itself. Then, when you call set_name($firstName,$lastName) you'll know that everything will be checked for you.











  • 13 years ago
    thanks. but i can make tha class acts like an array of struct right?

  • 13 years ago
    Of course you can, do a quick search on arrays in the php manual to find out how they work.

  • 13 years ago
    ytuui
  • 12 years ago

    sir can you help me construct a struct????enter 5 x and y coordinates store in an array struct

    displayed 5 "*" on the screen bassed on the coordinate..thanks

  • 11 years ago

    Hi andrew, I would like to know is it possible to call a C structure inside the PHP page. i have a page called simple.c

    include

    struct hits { int totalHits; int dailyHits; } nullhosting, metalshell;

    int main() { nullhosting.totalHits = 1000; nullhosting.dailyHits = 10;

        metalshell.totalHits = 3000;
        metalshell.dailyHits = 100;
    
        /* print metalshell's hits */
        printf("Metalshell.com's Stats:\n");
        printf("Total hits: %i Daily hits: %i\n\n", metalshell.totalHits, metalshell.dailyHits);
    
        /* print nullhosting's hits */
        printf("Nullhosting.com's Stats:\n");
        printf("Total hits: %i Daily Hits: %i\n", nullhosting.totalHits, nullhosting.dailyHits);
    
        return 0;
    

    }

    ......................

    I want to call this structure value (metalshell.totalHits = 3000;) to my PHP page.

    I am not sure it is possible or not. If it is possible could you please guide me how do get this value in a php page. Could you please send some sample code.

    thanks susikto

  • 11 years ago

    Hi andrew, I would like to know is it possible to call a C structure inside the PHP page. i have a page called simple.c

    include

    struct hits { int totalHits; int dailyHits; } nullhosting, metalshell;

    int main() { nullhosting.totalHits = 1000; nullhosting.dailyHits = 10;

        metalshell.totalHits = 3000;
        metalshell.dailyHits = 100;
    
        /* print metalshell's hits */
        printf("Metalshell.com's Stats:\n");
        printf("Total hits: %i Daily hits: %i\n\n", metalshell.totalHits, metalshell.dailyHits);
    
        /* print nullhosting's hits */
        printf("Nullhosting.com's Stats:\n");
        printf("Total hits: %i Daily Hits: %i\n", nullhosting.totalHits, nullhosting.dailyHits);
    
        return 0;
    

    }

    ......................

    I want to call this structure value (metalshell.totalHits = 3000;) to my PHP page.

    I am not sure it is possible or not. If it is possible could you please guide me how do get this value in a php page. Could you please send some sample code.

    thanks susikto

  • 11 years ago

    Something like the following will give you a class in php that acts similarly to the c structure that you provide.

    <?php
    class hits/* The Structure*/{
        public $totalHits;
        public $dailyHits;
    }
    
    $nullhosting = new hits();
    $metalshell = new hits();
    
    $metalshell->totalHits = 3000;
    $metalshell->dailyHits = 100;
    
    /*http://uk2.php.net/manual/en/function.sprintf.php*/
    
    printf("<p>Metalshell.com's Stats:\n<br/>");
    printf("Total hits: %d Daily hits: %d\n\n<br/></p>", $metalshell->totalHits, $metalshell->dailyHits);
    
    printf("<p>Nullhosting.com's Stats:\n<br/>");
    printf("Total hits: %d Daily Hits: %d\n<br/></p>", $nullhosting->totalHits, $nullhosting->dailyHits);
    ?>
    
  • 11 years ago

    Hi andrew,

    Thanks a lot lot for your kind reply. Actually what i want is, i have a simple.c file and inside that C file one a Data structure has define. like include

    struct hits { int totalHits; int dailyHits; } nullhosting, metalshell;

    int main() { nullhosting.totalHits = 1000; nullhosting.dailyHits = 10;

        metalshell.totalHits = 3000;
        metalshell.dailyHits = 100;
    
        /* print metalshell's hits */
        printf("Metalshell.com's Stats:\n");
        printf("Total hits: %i Daily hits: %i\n\n", metalshell.totalHits, metalshell.dailyHits);
    
        /* print nullhosting's hits */
        printf("Nullhosting.com's Stats:\n");
        printf("Total hits: %i Daily Hits: %i\n", nullhosting.totalHits, nullhosting.dailyHits);
    
        return 0;
    

    } ------------------------------------------------end c file-------------------------------------------------

    I would like to call this "struct hits" directly in my PHP page. Is it possible? I am using the msggetqueue() and msg_receive() function to get the structure value. But unfortunately i am not able to get the value of c:structures.

    please help me out.

    regards

    susikta

Post a reply

Enter your message below

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“The most exciting phrase to hear in science, the one that heralds new discoveries, is not 'Eureka!' but 'That's funny...'” - Isaac Asimov