{"id":742,"date":"2012-06-07T07:39:23","date_gmt":"2012-06-07T07:39:23","guid":{"rendered":"http:\/\/www.withinweb.com\/info\/?p=742"},"modified":"2012-06-20T13:23:23","modified_gmt":"2012-06-20T13:23:23","slug":"simple-class-definition-in-php","status":"publish","type":"post","link":"https:\/\/www.withinweb.com\/info\/simple-class-definition-in-php\/","title":{"rendered":"Simple class definition in PHP"},"content":{"rendered":"<p>Here is a simple class definition in PHP<\/p>\n<p>&lt;?php<\/p>\n<p>class Box {<\/p>\n<p>private $_length;<br \/>\nprivate $_width;<br \/>\nprivate $_height;<\/p>\n<p>public function Box($length, $width, $height) {<br \/>\n$this-&gt;_length = $length;<br \/>\n$this-&gt;_width = $width;<br \/>\n$this-&gt;_height = $height;<br \/>\n}<\/p>\n<p>public function volume() {<br \/>\nreturn $this-&gt;_length * $this-&gt;width * $this-&gt;_height;<br \/>\n}<\/p>\n<p>}<\/p>\n<p>\/\/Example of using the class<br \/>\n$myBox = new Box(20,10, 5);<br \/>\n$myVolume = $myBox-&gt;volume();<br \/>\necho($myVolume);<\/p>\n<p>?&gt;<\/p>\n<p>The definitions of each access modifier :<\/p>\n<p>\u2022<strong> public<\/strong>. Public members can be accessed both from outside an object by using $obj-&gt;publicMember and by accessing it from inside the myMethod method via the special $this variable (for example, $this-&gt;publicMember). If another class inherits a public member, the same rules apply, and it can be accessed both from outside the derived class\u2019s objects and from within its methods.<\/p>\n<p>\u2022 <strong>protected<\/strong>. Protected members can be accessed only from within an object\u2019s method\u2014for example, $this-&gt;protectedMember. If another class inherits a protected member, the same rules apply, and it can be accessed from within the derived object\u2019s methods via the special $this variable.<\/p>\n<p>\u2022 <strong>private.<\/strong> Private members are similar to protected members because they can be accessed only from within an object\u2019s method. However, they are also inaccessible from a derived object\u2019s methods. Because private properties aren\u2019t visible from inheriting classes, two related classes may declare the same private properties. Each class will see its own private copy, which are unrelated.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Here is a simple class definition in PHP &lt;?php class Box { private $_length; private $_width; private $_height; public function Box($length, $width, $height) { $this-&gt;_length = $length; $this-&gt;_width = $width; $this-&gt;_height = $height; } public function volume() { return $this-&gt;_length<span class=\"ellipsis\">&hellip;<\/span><\/p>\n<div class=\"read-more\"><a href=\"https:\/\/www.withinweb.com\/info\/simple-class-definition-in-php\/\">Read more <span class=\"screen-reader-text\">Simple class definition in PHP<\/span><span class=\"meta-nav\"> &#8250;<\/span><\/a><\/div>\n<p><!-- end of .read-more --><\/p>\n","protected":false},"author":40,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[15,6],"tags":[],"class_list":["post-742","post","type-post","status-publish","format-standard","hentry","category-classes","category-general-php"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Simple class definition in PHP - PHP Web Applications<\/title>\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.withinweb.com\/info\/simple-class-definition-in-php\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Simple class definition in PHP - PHP Web Applications\" \/>\n<meta property=\"og:description\" content=\"Here is a simple class definition in PHP &lt;?php class Box { private $_length; private $_width; private $_height; public function Box($length, $width, $height) { $this-&gt;_length = $length; $this-&gt;_width = $width; $this-&gt;_height = $height; } public function volume() { return $this-&gt;_length&hellip;Read more Simple class definition in PHP &#8250;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.withinweb.com\/info\/simple-class-definition-in-php\/\" \/>\n<meta property=\"og:site_name\" content=\"PHP Web Applications\" \/>\n<meta property=\"article:published_time\" content=\"2012-06-07T07:39:23+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2012-06-20T13:23:23+00:00\" \/>\n<meta name=\"author\" content=\"paulv\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"paulv\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.withinweb.com\\\/info\\\/simple-class-definition-in-php\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.withinweb.com\\\/info\\\/simple-class-definition-in-php\\\/\"},\"author\":{\"name\":\"paulv\",\"@id\":\"https:\\\/\\\/www.withinweb.com\\\/info\\\/#\\\/schema\\\/person\\\/04da5531c302d55ffcd777fe81dbb93c\"},\"headline\":\"Simple class definition in PHP\",\"datePublished\":\"2012-06-07T07:39:23+00:00\",\"dateModified\":\"2012-06-20T13:23:23+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.withinweb.com\\\/info\\\/simple-class-definition-in-php\\\/\"},\"wordCount\":245,\"commentCount\":0,\"articleSection\":[\"Classes\",\"General PHP\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.withinweb.com\\\/info\\\/simple-class-definition-in-php\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.withinweb.com\\\/info\\\/simple-class-definition-in-php\\\/\",\"url\":\"https:\\\/\\\/www.withinweb.com\\\/info\\\/simple-class-definition-in-php\\\/\",\"name\":\"Simple class definition in PHP - PHP Web Applications\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.withinweb.com\\\/info\\\/#website\"},\"datePublished\":\"2012-06-07T07:39:23+00:00\",\"dateModified\":\"2012-06-20T13:23:23+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/www.withinweb.com\\\/info\\\/#\\\/schema\\\/person\\\/04da5531c302d55ffcd777fe81dbb93c\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.withinweb.com\\\/info\\\/simple-class-definition-in-php\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.withinweb.com\\\/info\\\/simple-class-definition-in-php\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.withinweb.com\\\/info\\\/simple-class-definition-in-php\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.withinweb.com\\\/info\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Simple class definition in PHP\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.withinweb.com\\\/info\\\/#website\",\"url\":\"https:\\\/\\\/www.withinweb.com\\\/info\\\/\",\"name\":\"PHP Web Applications\",\"description\":\"Information and support for products of WithinWeb.com\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.withinweb.com\\\/info\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.withinweb.com\\\/info\\\/#\\\/schema\\\/person\\\/04da5531c302d55ffcd777fe81dbb93c\",\"name\":\"paulv\",\"url\":\"https:\\\/\\\/www.withinweb.com\\\/info\\\/author\\\/paulv\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Simple class definition in PHP - PHP Web Applications","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.withinweb.com\/info\/simple-class-definition-in-php\/","og_locale":"en_US","og_type":"article","og_title":"Simple class definition in PHP - PHP Web Applications","og_description":"Here is a simple class definition in PHP &lt;?php class Box { private $_length; private $_width; private $_height; public function Box($length, $width, $height) { $this-&gt;_length = $length; $this-&gt;_width = $width; $this-&gt;_height = $height; } public function volume() { return $this-&gt;_length&hellip;Read more Simple class definition in PHP &#8250;","og_url":"https:\/\/www.withinweb.com\/info\/simple-class-definition-in-php\/","og_site_name":"PHP Web Applications","article_published_time":"2012-06-07T07:39:23+00:00","article_modified_time":"2012-06-20T13:23:23+00:00","author":"paulv","twitter_card":"summary_large_image","twitter_misc":{"Written by":"paulv","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.withinweb.com\/info\/simple-class-definition-in-php\/#article","isPartOf":{"@id":"https:\/\/www.withinweb.com\/info\/simple-class-definition-in-php\/"},"author":{"name":"paulv","@id":"https:\/\/www.withinweb.com\/info\/#\/schema\/person\/04da5531c302d55ffcd777fe81dbb93c"},"headline":"Simple class definition in PHP","datePublished":"2012-06-07T07:39:23+00:00","dateModified":"2012-06-20T13:23:23+00:00","mainEntityOfPage":{"@id":"https:\/\/www.withinweb.com\/info\/simple-class-definition-in-php\/"},"wordCount":245,"commentCount":0,"articleSection":["Classes","General PHP"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.withinweb.com\/info\/simple-class-definition-in-php\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.withinweb.com\/info\/simple-class-definition-in-php\/","url":"https:\/\/www.withinweb.com\/info\/simple-class-definition-in-php\/","name":"Simple class definition in PHP - PHP Web Applications","isPartOf":{"@id":"https:\/\/www.withinweb.com\/info\/#website"},"datePublished":"2012-06-07T07:39:23+00:00","dateModified":"2012-06-20T13:23:23+00:00","author":{"@id":"https:\/\/www.withinweb.com\/info\/#\/schema\/person\/04da5531c302d55ffcd777fe81dbb93c"},"breadcrumb":{"@id":"https:\/\/www.withinweb.com\/info\/simple-class-definition-in-php\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.withinweb.com\/info\/simple-class-definition-in-php\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.withinweb.com\/info\/simple-class-definition-in-php\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.withinweb.com\/info\/"},{"@type":"ListItem","position":2,"name":"Simple class definition in PHP"}]},{"@type":"WebSite","@id":"https:\/\/www.withinweb.com\/info\/#website","url":"https:\/\/www.withinweb.com\/info\/","name":"PHP Web Applications","description":"Information and support for products of WithinWeb.com","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.withinweb.com\/info\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.withinweb.com\/info\/#\/schema\/person\/04da5531c302d55ffcd777fe81dbb93c","name":"paulv","url":"https:\/\/www.withinweb.com\/info\/author\/paulv\/"}]}},"_links":{"self":[{"href":"https:\/\/www.withinweb.com\/info\/wp-json\/wp\/v2\/posts\/742","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.withinweb.com\/info\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.withinweb.com\/info\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.withinweb.com\/info\/wp-json\/wp\/v2\/users\/40"}],"replies":[{"embeddable":true,"href":"https:\/\/www.withinweb.com\/info\/wp-json\/wp\/v2\/comments?post=742"}],"version-history":[{"count":4,"href":"https:\/\/www.withinweb.com\/info\/wp-json\/wp\/v2\/posts\/742\/revisions"}],"predecessor-version":[{"id":777,"href":"https:\/\/www.withinweb.com\/info\/wp-json\/wp\/v2\/posts\/742\/revisions\/777"}],"wp:attachment":[{"href":"https:\/\/www.withinweb.com\/info\/wp-json\/wp\/v2\/media?parent=742"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.withinweb.com\/info\/wp-json\/wp\/v2\/categories?post=742"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.withinweb.com\/info\/wp-json\/wp\/v2\/tags?post=742"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}