tulley Posted August 20, 2009 Report Share Posted August 20, 2009 experienced C++, new to php, question: I am following the tutorial at killerphp, using the following code.. <?php class person { var $name; function __construct($persons_name) { $this->name = $persons_name; } function set_name($new_name) { $this->name = $new_name; } function get_name() { return $this->name; } } ?> and... <?php $stefan = new person("Stefan"); echo "Stefan's full name: " . $stefan->get_name(); ?> The echo statement does not print the name set by the constructor. It works fine when using a set_name() function. What am I missing? I think I'm using version 4.x? of php. Quote Link to comment Share on other sites More sharing options...
Wickham Posted August 20, 2009 Report Share Posted August 20, 2009 As far as I know, OOP (object oriented PHP) was introduced with PHP 5 so perhaps that's why you cannot get it to work. However, on your computer you could download WampServer 2 or XAMPP which should have PHP 5 for testing locally. Quote Link to comment Share on other sites More sharing options...
falkencreative Posted August 20, 2009 Report Share Posted August 20, 2009 "__construct" is something that is new to PHP5, so if you are running php 4, you won't be able to use it. This doesn't mean you can't use Object Oriented programming at all with PHP4, you just have to do it a different way: class person { var $name; function person($persons_name) { $this->name = $persons_name; } function set_name($new_name) { $this->name = $new_name; } function get_name() { return $this->name; } } $stefan = new person("Stefan"); echo "Stefan's full name: " . $stefan->get_name(); ?> With PHP4, you can't use "__construct", your constructor needs to be the same name as the class (in this case, "function person"). Quote Link to comment 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.
Note: Your post will require moderator approval before it will be visible.