PHP OOP guide - Printable Version +- Support Forums (https://www.supportforums.net) +-- Forum: Categories (https://www.supportforums.net/forumdisplay.php?fid=87) +--- Forum: Coding Support Forums (https://www.supportforums.net/forumdisplay.php?fid=18) +---- Forum: PHP The Hypertext Preprocessor (https://www.supportforums.net/forumdisplay.php?fid=21) +---- Thread: PHP OOP guide (/showthread.php?tid=2761) |
PHP OOP guide - Gaijin - 11-09-2009 Good luck! Welcome Today I will show you what Object Oriented Programming (short: OOP) is, and how it works. If you just started learning PHP, I recommend you reading my PHP Beginners guide or Tim's PHP 101. For the case where you don't understand something, just ask in this thread. What is OOP http://wikipedia.org/wiki/Object-oriented_programming Wrote:Object-oriented programming (OOP) is a programming paradigm that uses "objects" – data structures consisting of datafields and methods together with their interactions – to design applications and computer programs. The Syntax To define a Object (Class), you must begin with the keyword class, after the definition keyword you give your Class a name. The Structure of the class is then written between a pair of curly braces. As it is with functions and variables, the name of the class can be anything you wish, but not any of PHP's reserved words. Letter or underscore are the only characters which are allowed as first character in the name. PHP Code: // class _tutClass : valid Properties Every class can have it's own variables, constants, functions, those are called properties and are defined with the keywords, var(DEPRECATED), public, private, protected, const. http://www.php.net/ Wrote:In order to maintain backward compatibility with PHP 4, PHP 5 will still accept the use of the keyword var in property declarations instead of (or in addition to) public, protected, or private. Read More PHP Code: class tutClass { Variables should be clear by now to you, so I will cover an CONSTANT a little. Constant is an name or identifier for a value, the value of an constant can't change during the execution of the script. Constants doesn't need the $ sign as variables when defining and accessing. As variables and function names, constants are case-sensitive. Static properties or methods can be accesssed without the need to initialize the class first. Methods: Methods are functions within your class, they are defined as properties with the keywords, public, private, protected and the keyword function. PHP Code: class tutClass { Access To access the properties and methods of your class, you need to create an instance Object of your class. The instance of an Object is created with the keyword new, following by the name of your Class. PHP Code: class tutClass { Within the class all preoprties and methods are accessed using the pseudo-variable $this, outside the class $this is represented by the instance variable, in the above code that would be the $obj variable. Following by -> and the name of the property or method, $this->admin would have the value "Omniscient". PHP Code: class tutClass { If you try to access your properties without the pseudo-variable, you will get "undefined variable" error. Let us create an instance. PHP Code: $obj = new tutClass; We have created two more properties, constant and static, those properties cannot be accessed with the pseudo-variable but only with the; Scope Resolution Operator "::": Read More. Constant and Static values: To access those values the are following forms. Form to access constants inside your class is: self::constant, parent::constant . Form to access static inside your class is: self::$static, parent::$static . Outside of your class those two values can be accessed with the name of your Class. In my tutorial the form would be: tutClass::constant and for static tutClass::$static PHP Code: $obj = new tutClass; Constant and static values doesn't require an instance of your class, in other words the line from the above script isn't needed. PHP Code: $obj = new tutClass; Extending your class: I will cover one more feature, you can combine two classes to work together and use each others properties and methods. That is being done when defining your class, with the keyword extends following by the name of the class that should be used. PHP Code: class FOO { As you can see in the above code, we are able to access properties and methods from one class within an other class. parent is used to access constant or static values from the class that is being imported in your class with the extends keyword. The following code will execute the function "showFOO()" from our BAR class, which then outputs properties from our extended class, public $foo and const Bar. PHP Code: $obj = new BAR; Constructor: You are able to define an constructor for your class, a constructor is run at each newly-created instance of your class. Constructor mostly contain code to initialize the class and define needed values. PHP Code: class tutClass { There are also; Destructors: I guess that you can imagine what they are used for. http://www.php.net Wrote:The destructor method will be called as soon as all references to a particular object are removed or when the object is explicitly destroyed or in any order in shutdown sequence. PHP Code: class tutClass { Read More PHP OOP Thank you for reading! RE: PHP OOP guide - zone - 11-10-2009 Fantastic, I learnt many new points which I can't know before. Thanks Ninja RE: PHP OOP guide - Gaijin - 11-10-2009 (11-10-2009, 01:51 AM)zone Wrote: Fantastic, I learnt many new points which I can't know before. Thanks Ninja Thank you very much, I'm glad you learned something. RE: PHP OOP guide - Jamza - 11-10-2009 Thanks for this, I was actually needing something like this almost at the very moment you posted it. Id rep you if i could RE: PHP OOP guide - Gaijin - 11-10-2009 (11-10-2009, 04:48 AM)Jamza Wrote: Thanks for this, I was actually needing something like this almost at the very moment you posted it. Thank you too, and don't worry about REP. I'm glad if I helped you! |