Jump to content

Recommended Posts

so im trying to build a class in php and all i have experience in really is Java.

 

how would i go about doing something like....where i set some variables right when i create an element

 

public element(int i, string s)

{

    public int integer=i;

    public string string1=s;

}

 

so i can use it like

 

element e = new element(1,"hello");

Link to comment
https://forums.phpfreaks.com/topic/122467-solved-difference-in-java-and-php/
Share on other sites

<?php

class element {

    public $i, $s;

    public function __construct( $i, $s ) {
        $this->i = $i;
        $this->s = $s;
    }
    
    public function foobar( $var ) {
      return "Hello $var! <br /> Your int was {$this->i} and your string was {$this->s}";
    }

}

$obj = new element( 1, 'hello' );

echo $obj->i .'<br />'. $obj->s .'<br />'. $obj->foobar( 'world' );

?>

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.