ultrus Posted May 29, 2007 Share Posted May 29, 2007 Hello, I'm a bit new on using classes in php, but not for long! I'm running into error messages when using the following code: class Sapi { var $voiceObj; function Sapi($voiceObj) { $this->$voiceObj = new COM("SAPI.SpVoice"); } function speak($text) { $this->$voiceObj->Speak($text); } } $tts = new Sapi(); Error message: "Warning: Missing argument 1 for sapi() in /home/content/u/l/t/ultrus/html/sapi.php on line 11" The following simplified example brings the same error message: class Sapi { var $voiceObj; function Sapi($voiceObj) { $this->$voiceObj = "hello"; } } $tts = new Sapi(); Any idea on how to fix this? I think I'm close. Thanks in advance Quote Link to comment https://forums.phpfreaks.com/topic/53413-php-class-having-trouble-with-structuring/ Share on other sites More sharing options...
Wildbug Posted May 29, 2007 Share Posted May 29, 2007 class Sapi { var $voiceObj; function Sapi($voiceObj) { // <-- Your function is expecting an argument here: $voiceObj $this->$voiceObj = "hello"; } } $tts = new Sapi(); // <-- ...but you haven't provided one here. Quote Link to comment https://forums.phpfreaks.com/topic/53413-php-class-having-trouble-with-structuring/#findComment-263919 Share on other sites More sharing options...
ultrus Posted May 29, 2007 Author Share Posted May 29, 2007 ah! I don't really need an argument, and this works great: class Sapi { var $voiceObj; function Sapi() { $this->$voiceObj = "hello"; } } $tts = new Sapi(); Thanks much Wilbug Quote Link to comment https://forums.phpfreaks.com/topic/53413-php-class-having-trouble-with-structuring/#findComment-263924 Share on other sites More sharing options...
Wildbug Posted May 29, 2007 Share Posted May 29, 2007 Incidently, PHP has a neat feature that allows optional arguments with a default value. <?php function test($arg1 = '') {return strtolower($arg1);} test(); ?> I should have mentioned that earlier, but your example didn't really need an argument. Good luck with OOP! Quote Link to comment https://forums.phpfreaks.com/topic/53413-php-class-having-trouble-with-structuring/#findComment-263926 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.