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"); 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' ); ?> 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 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
Archived
This topic is now archived and is closed to further replies.