Alphamonkey Posted September 3, 2008 Share Posted September 3, 2008 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"); Quote Link to comment https://forums.phpfreaks.com/topic/122467-solved-difference-in-java-and-php/ Share on other sites More sharing options...
discomatt Posted September 3, 2008 Share Posted September 3, 2008 <?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' ); ?> Quote Link to comment https://forums.phpfreaks.com/topic/122467-solved-difference-in-java-and-php/#findComment-632365 Share on other sites More sharing options...
Alphamonkey Posted September 3, 2008 Author Share Posted September 3, 2008 Quote Link to comment https://forums.phpfreaks.com/topic/122467-solved-difference-in-java-and-php/#findComment-632373 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.