Jump to content

[SOLVED] difference in java and php


Alphamonkey

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' );

?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.