{"id":14177,"date":"2016-08-01T16:15:01","date_gmt":"2016-08-01T13:15:01","guid":{"rendered":"http:\/\/www.webcodegeeks.com\/?p=14177"},"modified":"2018-01-09T10:47:54","modified_gmt":"2018-01-09T08:47:54","slug":"php-class-example","status":"publish","type":"post","link":"https:\/\/www.webcodegeeks.com\/php\/php-class-example\/","title":{"rendered":"PHP class Example"},"content":{"rendered":"<p>In this example we are going to learn about classes in PHP, what they mean and how to use them.<\/p>\n<ol>\n<li>A computer with PHP 5.5 installed<\/li>\n<li>notepad++<\/li>\n<\/ol>\n<h2>1. Objects in PHP<\/h2>\n<p>A class is a collection of variables and functions working with these variables.<br \/>\nTo effectively use classes we need to familiarize ourselves with the term objects. Classes can be useless without objects. To use a class we might have to create an instance of the class, the instance created is the object.<\/p>\n<p>If we have a class named <code> employee<\/code> that holds details about each employee in an organization, to instantiate this class we simply do <code>$worker=new employee(); <\/code> . <code>$worker<\/code> is an object and an instance of employee.<br \/>\nYou can think of a class as a blueprint or factory for constructing objects. A class specifies the characteristics that an object would have, but not necessarily the specific values of those characteristics.<br \/>\n&nbsp;<br \/>\n[ulp id=&#8217;8njY7i2QRy6sg8pg&#8217;]<\/p>\n<h2>1.2 Properties and Methods<\/h2>\n<p>In OOP(Object Oriented Programming) the characteristics of a class or object is known as it&#8217;s properties. Properties are variables (they have a name and value). Using an employee class as an example, the name of the employee is a property of the class or any object of the class.<br \/>\nMethods are simply the behavior of the class-actions associated with the class. Methods are somewhat like functions, the difference is that methods are associated with classes and objects.<\/p>\n<h2>1.3 Creating Classes<\/h2>\n<p>As you already imagined it is impossible to create an object without a class. To create a class in PHP you use the class keyword<\/p>\n<p><span style=\"text-decoration: underline;\"><em>employee.php<\/em><\/span><\/p>\n<pre class=\"brush:php;\">&lt;?php\r\n\r\nclass employee{\r\npublic $name;\r\n\r\n\r\n}\r\n$worker=new employee();\r\n\r\n?&gt;\r\n<\/pre>\n<p>This is a simple class named <code>employee<\/code>, with property <code>$name<\/code>. To add a property to a class first we write the keyword <code>public<\/code> <code>protected<\/code> or <code>private<\/code> followed by the property name. The keyword we use to declare a property controls the scope or visibility of the property.<\/p>\n<p><span style=\"text-decoration: underline;\"><em>employee1.php<\/em><\/span><\/p>\n<pre class=\"brush:php;\">&lt;?php\r\nclass employee{\r\npublic $name=\"John\";\r\n\r\n\r\n}\r\n$worker=new employee();\r\necho $worker-&gt;name;\r\n?&gt;\r\n<\/pre>\n<p>To access an object property we write the name of the variable storing the object, followed by an arrow symbol composed a hyphen(-) and a greater than symbol (&gt;), followed by the property name(the property name doesn&#8217;t have the $ sign before it).<\/p>\n<p><span style=\"text-decoration: underline;\"><em>employee2.php<\/em><\/span><\/p>\n<pre class=\"brush:php;\">&lt;!DOCTYPE html&gt; \r\n&lt;html lang=en&gt;\r\n\t&lt;head&gt;\r\n\t&lt;style&gt;\r\n\thtml, body{\r\n\twidth:100%;\r\n\theight:100%;\r\n\tmargin:0%;\r\n\tfont-family:\"helvetica\",\"verdana\",\"calibri\", \"san serif\";\r\n\toverflow:hidden;\r\n\tpadding:0%;\r\n\tborder:0%;\r\n\t}\r\n\t \r\n\t&lt;\/style&gt;\r\n\t \t\t&lt;meta charset=\"utf-8\" \/&gt;\r\n\t\t&lt;meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, target-densitydpi=device-dpi\"\/&gt;\r\n\t\r\n\t&lt;title&gt;Employee Class&lt;\/title&gt;\r\n\t\r\n\t&lt;\/head&gt;\r\n\t&lt;body&gt;\r\n&lt;?php\r\n\r\nclass employee{\r\npublic $firstname=\"John\";\r\npublic $hasbeenpaid=false; \r\npublic $lastname=\"White\";\r\nprivate $salary=1000;\r\n}\r\n$worker=new employee();\r\nif($worker-&gt;hasbeenpaid)\r\necho \"$worker-&gt;firstname $worker-&gt;lastname has been paid $worker-&gt;salary\";\r\nelse\r\necho \"$worker-&gt;firstname $worker-&gt;lastname has not been paid $worker-&gt;salary\";\r\n\r\n?&gt;\r\n&lt;\/body&gt;\r\n&lt;\/html&gt;\r\n<\/pre>\n<p>If this page is loaded In a browser, an error message is generated and displayed by PHP. The error message is shown because we tried to access a private property outside its class. A property declared private cannot be accessed from outside the class it was created(<code>$salary<\/code> is private to the class <code>employee<\/code>) but a property declared public can be accessed from outside the class it was created.<br \/>\nHow do we manipulate a private field in PHP? By simply creating a method that returns the value of the private field.<\/p>\n<p><span style=\"text-decoration: underline;\"><em>employee3.php<\/em><\/span><\/p>\n<pre class=\"brush:php;\">&lt;!DOCTYPE html&gt; \r\n&lt;html lang=en&gt;\r\n\t&lt;head&gt;\r\n\t&lt;style&gt;\r\n\thtml, body{\r\n\twidth:100%;\r\n\theight:100%;\r\n\tmargin:0%;\r\n\tfont-family:\"helvetica\",\"verdana\",\"calibri\", \"san serif\";\r\n\toverflow:hidden;\r\n\tpadding:0%;\r\n\tborder:0%;\r\n\t}\r\n\t \r\n\t&lt;\/style&gt;\r\n\t \t\t&lt;meta charset=\"utf-8\" \/&gt;\r\n\t\t&lt;meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, target-densitydpi=device-dpi\"\/&gt;\r\n\t\r\n\t&lt;title&gt;Employee Class&lt;\/title&gt;\r\n\t\r\n\t&lt;\/head&gt;\r\n\t&lt;body&gt;\r\n&lt;?php\r\n\r\nclass employee{\r\npublic $firstname=\"John\";\r\npublic $hasbeenpaid=false; \r\npublic $lastname=\"White\";\r\nprivate $salary=1000;\r\n\r\npublic function getSalary(){\r\nreturn $this-&gt;salary;\r\n\r\n}\r\n}\r\n$worker=new employee();\r\nif($worker-&gt;hasbeenpaid){\r\necho \"$worker-&gt;firstname $worker-&gt;lastname has been paid\" . $worker-&gt;getSalary();}\r\nelse{\r\necho \"$worker-&gt;firstname $worker-&gt;lastname has not been paid\" . $worker-&gt;getSalary();\r\n}\r\n\r\n?&gt;\r\n&lt;\/body&gt;\r\n&lt;\/html&gt;\r\n\r\n\r\n<\/pre>\n<p>We created a method <code>getSalary()<\/code> which returns the value of the private field <code>$salary<\/code><\/p>\n<h2>1.4 Static Properties and Constants<\/h2>\n<p>When a field is declared to be static it becomes a member of the class, where it was created (it is not associated with any object of that class). Static members are independent of any particular object derived from the class. To declare a property static, the keyword <code>static<\/code> is used<br \/>\n<code> public static $number<\/code><br \/>\nTo access a static property the class name is written followed by two colons(::) followed by the property name preceded by the $ symbol<br \/>\n<code>MyClass::$number<\/code><br \/>\nConstants are fixed values that do not change throughout the running of a script (once it has has been initialized, it&#8217;s value can&#8217;t be changed). To declare a class variable as a constant, the <code>const<\/code> keyword is used<br \/>\n<code>const VALUE=5;<\/code><br \/>\nIt is good practice to use all-uppercase letters for a constant name. We access a constant value same way we do for a static property<br \/>\n<code>MyClass::VALUE<\/code><\/p>\n<h2>1.5 Methods<\/h2>\n<p>Method are functions described in a class. To add a method to a class use the public, private or protected keyword, then the function keyword, followed by the method name, followed by parantheses. You then include the method code within curly braces. The keywords <code>protected<\/code> <code>private<\/code> and <code>public<\/code> can be left out, if this is done, PHP uses keyword <code>public<\/code> as the default.<\/p>\n<p>To call an object method, simply write the object name, then the same arrow used for accessing properties(-&gt;) then the method name followed by paranthesis<\/p>\n<pre class=\"brush:bash\">$worker-&gt;getSalary();<\/pre>\n<p>We can also add or pass parameters to a method, so it can accept arguments to manipulate.<\/p>\n<p><span style=\"text-decoration: underline;\"><em>employee4.php<\/em><\/span><\/p>\n<pre class=\"brush:php; highlight:[32,46]\">&lt;!DOCTYPE html&gt; \r\n&lt;html lang=en&gt;\r\n\t&lt;head&gt;\r\n\t&lt;style&gt;\r\n\thtml, body{\r\n\twidth:100%;\r\n\theight:100%;\r\n\tmargin:0%;\r\n\tfont-family:\"helvetica\",\"verdana\",\"calibri\", \"san serif\";\r\n\toverflow:hidden;\r\n\tpadding:0%;\r\n\tborder:0%;\r\n\t}\r\n\t \r\n\t&lt;\/style&gt;\r\n\t \t\t&lt;meta charset=\"utf-8\" \/&gt;\r\n\t\t&lt;meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, target-densitydpi=device-dpi\"\/&gt;\r\n\t\r\n\t&lt;title&gt;Employee Class&lt;\/title&gt;\r\n\t\r\n\t&lt;\/head&gt;\r\n\t&lt;body&gt;\r\n&lt;?php\r\n\r\nclass employee{\r\npublic $firstname=\"John\";\r\npublic $hasbeenpaid=false; \r\npublic $lastname=\"White\";\r\nprivate $salary=1000;\r\n\/\/ this method can be accessed outside the class\r\npublic function getSalary(){\r\nreturn $this-&gt;salary;\r\n\r\n}\r\n\/\/this functuon can only be accessed within the class\r\nprivate function doSomeThing(){\r\n\r\n}\r\n\r\nprotected function doSomeThings(){\r\n\/\/ can be accessed within the class and any class that extend this class\r\n\r\n}\r\npublic function increaseSalary($amount){\r\n\r\n$this-&gt;salary=$this-&gt;salary . $amount;\r\necho \"Salary has been increased\";\r\n\r\n}\r\n}\r\n\r\n$worker=new employee();\r\n$worker-&gt;increaseSalary(1000);\r\n\r\n?&gt;\r\n&lt;\/body&gt;\r\n&lt;\/html&gt;\r\n<\/pre>\n<p>Edit the code above by trying to call methods that are private or protected and see how PHP responds. To access an object property from a method <code>$this<\/code> keyword is used(Line 32 and 46).<\/p>\n<h2>1.6 Constructors<\/h2>\n<p>When creating a new object it might be useful to set up certain aspects of the object at the same time. For example when creating an employee object, we might pass the employee name and position occupied, so the object can be fully initialized.<\/p>\n<p>To create a constructor in PHP, simply add a method with the spcial name <code>_construct()<\/code> to your class(two underscores followed by the word construct). PHP looks for this special method name when the object is created; if it finds it, it calls the method. You can pass parameters to constructors, just like normal methods, this is great for setting certain properties to initial values as the object is created.<\/p>\n<p><span style=\"text-decoration: underline;\"><em>employee5.php<\/em><\/span><\/p>\n<pre class=\"brush:php; highlight:[62]\">&lt;!DOCTYPE html&gt; \r\n&lt;html lang=en&gt;\r\n\t&lt;head&gt;\r\n\t&lt;style&gt;\r\n\thtml, body{\r\n\twidth:100%;\r\n\theight:100%;\r\n\tmargin:0%;\r\n\tfont-family:\"helvetica\",\"verdana\",\"calibri\", \"san serif\";\r\n\toverflow:hidden;\r\n\tpadding:0%;\r\n\tborder:0%;\r\n\t}\r\n\t \r\n\t&lt;\/style&gt;\r\n\t \t\t&lt;meta charset=\"utf-8\" \/&gt;\r\n\t\t&lt;meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, target-densitydpi=device-dpi\"\/&gt;\r\n\t\r\n\t&lt;title&gt;Employee Class&lt;\/title&gt;\r\n\t\r\n\t&lt;\/head&gt;\r\n\t&lt;body&gt;\r\n&lt;?php\r\n\r\nclass employee{\r\npublic $firstname;\r\npublic $hasbeenpaid=false; \r\npublic $lastname;\r\nprivate $salary=1000;\r\n\r\npublic function __construct($first,$last){\r\n$this-&gt;firstname=$first;\r\n$this-&gt;lastname=$last;\r\n}\r\n\/\/ this method can be accessed outside the class\r\npublic function getSalary(){\r\nreturn $this-&gt;salary;\r\n\r\n}\r\n\/\/this functuon can only be accessed within the class\r\nprivate function doSomeThing(){\r\n\r\n}\r\n\r\nprotected function doSomeThings(){\r\n\/\/ can be accessed within the class and any class that extend this class\r\n\r\n}\r\npublic function increaseSalary($amount){\r\n\r\n$this-&gt;salary=$this-&gt;salary . $amount;\r\necho \"Salary has been increased\";\r\n\r\n}\r\nfunction printUserDetails(){\r\necho \" FirstName Is $this-&gt;firstname &lt;br&gt;\";\r\necho \"LastName Is $this-&gt;lastname &lt;br&gt;\";\r\n\r\n}\r\n}\r\n\r\n$worker=new employee(\"Jack\",\"Daniels\");\r\n$worker-&gt;printUserDetails();\r\n\r\n?&gt;\r\n&lt;\/body&gt;\r\n&lt;\/html&gt;\r\n<\/pre>\n<p>On line 62 we initialize the object by passing arguments firstname and lastname to the class constructor.<\/p>\n<h2>2. Summary<\/h2>\n<p>In this example we have learnt about classes, methods,static properties, class constants and constructors, how to create and manipulate them.<\/p>\n<h2>3. Download the source code<\/h2>\n<div class=\"download\"><strong>Download<\/strong><br \/>\nYou can download the full source code of this example here: <strong><a href=\"http:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2016\/07\/Phpclassexample.zip\">Phpclassexample<\/a><\/strong><\/div>\n","protected":false},"excerpt":{"rendered":"<p>In this example we are going to learn about classes in PHP, what they mean and how to use them. A computer with PHP 5.5 installed notepad++ 1. Objects in PHP A class is a collection of variables and functions working with these variables. To effectively use classes we need to familiarize ourselves with the &hellip;<\/p>\n","protected":false},"author":164,"featured_media":930,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[10],"tags":[],"class_list":["post-14177","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-php"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>PHP class Example - Web Code Geeks - 2026<\/title>\n<meta name=\"description\" content=\"In this example we are going to learn about classes in PHP, what they mean and how to use them. A computer with PHP 5.5 installed notepad++ 1. Objects in\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.webcodegeeks.com\/php\/php-class-example\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"PHP class Example - Web Code Geeks - 2026\" \/>\n<meta property=\"og:description\" content=\"In this example we are going to learn about classes in PHP, what they mean and how to use them. A computer with PHP 5.5 installed notepad++ 1. Objects in\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.webcodegeeks.com\/php\/php-class-example\/\" \/>\n<meta property=\"og:site_name\" content=\"Web Code Geeks\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/webcodegeeks\" \/>\n<meta property=\"article:published_time\" content=\"2016-08-01T13:15:01+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2018-01-09T08:47:54+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/php-logo.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"150\" \/>\n\t<meta property=\"og:image:height\" content=\"150\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Olayemi Odunayo\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@webcodegeeks\" \/>\n<meta name=\"twitter:site\" content=\"@webcodegeeks\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Olayemi Odunayo\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"8 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.webcodegeeks.com\/php\/php-class-example\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/php\/php-class-example\/\"},\"author\":{\"name\":\"Olayemi Odunayo\",\"@id\":\"https:\/\/www.webcodegeeks.com\/#\/schema\/person\/417918d9b5811210265e8590509718b8\"},\"headline\":\"PHP class Example\",\"datePublished\":\"2016-08-01T13:15:01+00:00\",\"dateModified\":\"2018-01-09T08:47:54+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/php\/php-class-example\/\"},\"wordCount\":910,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/#organization\"},\"image\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/php\/php-class-example\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/php-logo.jpg\",\"articleSection\":[\"PHP\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/www.webcodegeeks.com\/php\/php-class-example\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.webcodegeeks.com\/php\/php-class-example\/\",\"url\":\"https:\/\/www.webcodegeeks.com\/php\/php-class-example\/\",\"name\":\"PHP class Example - Web Code Geeks - 2026\",\"isPartOf\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/php\/php-class-example\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/php\/php-class-example\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/php-logo.jpg\",\"datePublished\":\"2016-08-01T13:15:01+00:00\",\"dateModified\":\"2018-01-09T08:47:54+00:00\",\"description\":\"In this example we are going to learn about classes in PHP, what they mean and how to use them. A computer with PHP 5.5 installed notepad++ 1. Objects in\",\"breadcrumb\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/php\/php-class-example\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.webcodegeeks.com\/php\/php-class-example\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.webcodegeeks.com\/php\/php-class-example\/#primaryimage\",\"url\":\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/php-logo.jpg\",\"contentUrl\":\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/php-logo.jpg\",\"width\":150,\"height\":150},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.webcodegeeks.com\/php\/php-class-example\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.webcodegeeks.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"PHP\",\"item\":\"https:\/\/www.webcodegeeks.com\/category\/php\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"PHP class Example\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.webcodegeeks.com\/#website\",\"url\":\"https:\/\/www.webcodegeeks.com\/\",\"name\":\"Web Code Geeks\",\"description\":\"Web Developers Resource Center\",\"publisher\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.webcodegeeks.com\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.webcodegeeks.com\/#organization\",\"name\":\"Exelixis Media P.C.\",\"url\":\"https:\/\/www.webcodegeeks.com\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.webcodegeeks.com\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png\",\"contentUrl\":\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png\",\"width\":864,\"height\":246,\"caption\":\"Exelixis Media P.C.\"},\"image\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/webcodegeeks\",\"https:\/\/x.com\/webcodegeeks\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.webcodegeeks.com\/#\/schema\/person\/417918d9b5811210265e8590509718b8\",\"name\":\"Olayemi Odunayo\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.webcodegeeks.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/016b2a353262962fceecf5c274161cd9ef60fdf1593ec95e71d37f5fd9b444f6?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/016b2a353262962fceecf5c274161cd9ef60fdf1593ec95e71d37f5fd9b444f6?s=96&d=mm&r=g\",\"caption\":\"Olayemi Odunayo\"},\"description\":\"I am a programmer and web developer, who has experience in developing websites and writing desktop and mobile applications. I have worked with both schematic and schemaless databases. I am also familiar with third party API and working with cloud servers. I do programming on the client side with Java, JavaScript, html, Ajax and CSS while I use PHP for server side programming.\",\"sameAs\":[\"https:\/\/www.webcodegeeks.com\/\"],\"url\":\"https:\/\/www.webcodegeeks.com\/author\/olayemi-odunayo\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"PHP class Example - Web Code Geeks - 2026","description":"In this example we are going to learn about classes in PHP, what they mean and how to use them. A computer with PHP 5.5 installed notepad++ 1. Objects in","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.webcodegeeks.com\/php\/php-class-example\/","og_locale":"en_US","og_type":"article","og_title":"PHP class Example - Web Code Geeks - 2026","og_description":"In this example we are going to learn about classes in PHP, what they mean and how to use them. A computer with PHP 5.5 installed notepad++ 1. Objects in","og_url":"https:\/\/www.webcodegeeks.com\/php\/php-class-example\/","og_site_name":"Web Code Geeks","article_publisher":"https:\/\/www.facebook.com\/webcodegeeks","article_published_time":"2016-08-01T13:15:01+00:00","article_modified_time":"2018-01-09T08:47:54+00:00","og_image":[{"width":150,"height":150,"url":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/php-logo.jpg","type":"image\/jpeg"}],"author":"Olayemi Odunayo","twitter_card":"summary_large_image","twitter_creator":"@webcodegeeks","twitter_site":"@webcodegeeks","twitter_misc":{"Written by":"Olayemi Odunayo","Est. reading time":"8 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.webcodegeeks.com\/php\/php-class-example\/#article","isPartOf":{"@id":"https:\/\/www.webcodegeeks.com\/php\/php-class-example\/"},"author":{"name":"Olayemi Odunayo","@id":"https:\/\/www.webcodegeeks.com\/#\/schema\/person\/417918d9b5811210265e8590509718b8"},"headline":"PHP class Example","datePublished":"2016-08-01T13:15:01+00:00","dateModified":"2018-01-09T08:47:54+00:00","mainEntityOfPage":{"@id":"https:\/\/www.webcodegeeks.com\/php\/php-class-example\/"},"wordCount":910,"commentCount":0,"publisher":{"@id":"https:\/\/www.webcodegeeks.com\/#organization"},"image":{"@id":"https:\/\/www.webcodegeeks.com\/php\/php-class-example\/#primaryimage"},"thumbnailUrl":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/php-logo.jpg","articleSection":["PHP"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.webcodegeeks.com\/php\/php-class-example\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.webcodegeeks.com\/php\/php-class-example\/","url":"https:\/\/www.webcodegeeks.com\/php\/php-class-example\/","name":"PHP class Example - Web Code Geeks - 2026","isPartOf":{"@id":"https:\/\/www.webcodegeeks.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.webcodegeeks.com\/php\/php-class-example\/#primaryimage"},"image":{"@id":"https:\/\/www.webcodegeeks.com\/php\/php-class-example\/#primaryimage"},"thumbnailUrl":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/php-logo.jpg","datePublished":"2016-08-01T13:15:01+00:00","dateModified":"2018-01-09T08:47:54+00:00","description":"In this example we are going to learn about classes in PHP, what they mean and how to use them. A computer with PHP 5.5 installed notepad++ 1. Objects in","breadcrumb":{"@id":"https:\/\/www.webcodegeeks.com\/php\/php-class-example\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.webcodegeeks.com\/php\/php-class-example\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.webcodegeeks.com\/php\/php-class-example\/#primaryimage","url":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/php-logo.jpg","contentUrl":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/php-logo.jpg","width":150,"height":150},{"@type":"BreadcrumbList","@id":"https:\/\/www.webcodegeeks.com\/php\/php-class-example\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.webcodegeeks.com\/"},{"@type":"ListItem","position":2,"name":"PHP","item":"https:\/\/www.webcodegeeks.com\/category\/php\/"},{"@type":"ListItem","position":3,"name":"PHP class Example"}]},{"@type":"WebSite","@id":"https:\/\/www.webcodegeeks.com\/#website","url":"https:\/\/www.webcodegeeks.com\/","name":"Web Code Geeks","description":"Web Developers Resource Center","publisher":{"@id":"https:\/\/www.webcodegeeks.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.webcodegeeks.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.webcodegeeks.com\/#organization","name":"Exelixis Media P.C.","url":"https:\/\/www.webcodegeeks.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.webcodegeeks.com\/#\/schema\/logo\/image\/","url":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","contentUrl":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","width":864,"height":246,"caption":"Exelixis Media P.C."},"image":{"@id":"https:\/\/www.webcodegeeks.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/webcodegeeks","https:\/\/x.com\/webcodegeeks"]},{"@type":"Person","@id":"https:\/\/www.webcodegeeks.com\/#\/schema\/person\/417918d9b5811210265e8590509718b8","name":"Olayemi Odunayo","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.webcodegeeks.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/016b2a353262962fceecf5c274161cd9ef60fdf1593ec95e71d37f5fd9b444f6?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/016b2a353262962fceecf5c274161cd9ef60fdf1593ec95e71d37f5fd9b444f6?s=96&d=mm&r=g","caption":"Olayemi Odunayo"},"description":"I am a programmer and web developer, who has experience in developing websites and writing desktop and mobile applications. I have worked with both schematic and schemaless databases. I am also familiar with third party API and working with cloud servers. I do programming on the client side with Java, JavaScript, html, Ajax and CSS while I use PHP for server side programming.","sameAs":["https:\/\/www.webcodegeeks.com\/"],"url":"https:\/\/www.webcodegeeks.com\/author\/olayemi-odunayo\/"}]}},"_links":{"self":[{"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/posts\/14177","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/users\/164"}],"replies":[{"embeddable":true,"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/comments?post=14177"}],"version-history":[{"count":0,"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/posts\/14177\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/media\/930"}],"wp:attachment":[{"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/media?parent=14177"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/categories?post=14177"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/tags?post=14177"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}