PHP: Real world OOP example

后端 未结 7 645
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-04 05:53

I am trying to learn OOP. The so called \'real world\' examples in the books I am reading aren\'t helping.

All the examples like Pet, Car

相关标签:
7条回答
  • 2020-12-04 06:57

    I'd advise you to stay away from any framework at this moment, if you do not know OOP, digging into zend or any other framework would be too much.

    PHP OOP is quit funny... like ha ha funny, because it's supported, but PHP is not an OOP language like java or c#.

    Short example just to underline my statement:

    // define class
    class User {
    // define properties and methods
    public $name = "";
    }
    // instantiate class
    $user = new User; // or new User() or new user, it's all the same
    echo $user->name;
    

    but if you want to do OOP "on the fly" you can do the following:

    $user = (object) array('name' => 'Peter');
    

    and then

    $user->name;
    

    but you can use OOP like you would in java or c# but not to the same extend - and have in mind, popular systems like wordpress and drupal are not pure OOP! but you can do inheritance and other classing OOP stuff in PHP as well.

    0 讨论(0)
提交回复
热议问题