Topic: Beginners OO PHP step 14

I am working through the PHP OOP tutorial... I got stuck on step 14, __construct, as the output from the example is only: "Stephan's full name: " with no response to $stephan->get_name;

Thanks for this great site, I am looking forward to working my way through...

(I tried several searches of the forum and could not find an answer)

Vote up Vote down

Re: Beginners OO PHP step 14

I'd suggest creating a new .php file, with these contents:

<?php phpinfo(); ?>

and running it on your server. (you may have already done this in a previous lesson, but I'm not sure if Stefan covered that). If you have PHP 4, you won't be able to use "__construct", since that is PHP5 only.

If you are using a program like WAMP or MAMP to run a server on your local machine, chances are, you probably have PHP5. If you are running this on a live server, it is possible that your host hasn't upgraded to PHP5 yet.

If you are using PHP4, you can still use OOP, you just have to use a slightly different constructor. Instead of using "__construct", your constructor method will have the same name as the class (in this case, your constructor will be person()):

<?php         
    class person {
        var $name;

        function person($persons_name) {        <-- constructor
            $this->name = $persons_name;        
        }        
        
        function set_name($new_name) {
              $this->name = $new_name;
        }    
    
        function get_name() {        
              return $this->name;        
         }        

    }         
?>         

If you are running PHP5, and the "__construct" itself isn't a problem, post the code you are working with -- perhaps you have some sort of error that I will be able to catch.

Benjamin Falk | Falken Creative : Twitter
Skills: Photoshop, Illustrator, HTML, CSS, jQuery, PHP and CodeIgniter

Vote up Vote down

Re: Beginners OO PHP step 14

First:

XAMPP server w/ PHP 5.3.0
Apache 2.2.12 (win 32)
MySQL 5.1.37
NuSphere PHP Ed 5.8

During my study of your example I found my mistake: function __construct("THIS WAS EMPTY") { ...  Duh! I have been typing all the examples out instead of just copying them }

Now I am off and running again... While I was meditating on these things the light came on!!!

Class = (any) Person, Customer could be a type of person and Employee is a Type (sub-class) of person.  Manager could be a type of employee, as could assistant manager and so on depending how your business is structured smile

Thank you so much for your time and effort,

Ministermark at wordunplugged dot com

Vote up Vote down