• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
seegatesite header

Seegatesite.com

Seegatesite.com - Programming Tutorial , Sharing , How and Learn Together

  • TOOLS
    • Bootstrap Navbar Online Generator
    • Customize Sidebar Menu Bootstrap 3
    • Bootstrap Demo
  • ADVERTISE
  • CATEGORIES
    • Android
    • Blogging Tips
    • Database
    • CSS
    • Info Gadget
    • Javascript
    • Linux
    • PHP
    • Various
    • WordPress
  • Q&A
  • PHP
  • JAVASCRIPT
  • JQUERY
  • ANGULAR
  • WORDPRESS
  • SEO
  • REACT
🏠 » PHP » Basic Class in Object Oriented Programming PHP

Basic Class in Object Oriented Programming PHP

By Sigit Prasetya Nugroho ∙ January 31, 2015 ∙ PHP ∙ Leave a Comment

Share : TwitterFacebookTelegramWhatsapp

To create a new data type, you can use the CLASS keyword followed by the class name, where the class name must be a string. The class name must not be the same, with the name of the keyword in the PHP library. It should be noted that different from the the object class, where class is a new data type while the object is an instance / example of a utilization data type.

Basic Class in Object Oriented Programming PHP

Consider the following class declaration

1
2
3
4
class name_class
{
// attribute/method
}

Where class is a keyword that must be used when creating a new class, class_name is name of the class. Then after the class name created,please use curly brackets { , and ends with a closing curly brackets }.

Related Articles :

  • How To Create Custom Class In Laravel 5.5 For Beginners

1
2
3
4
class Cat
{
// attribute/method
}

Class is a new data type and consists of several data and functions. consider the following example

1
2
3
4
5
6
7
8
class Cat
{
$name;
        $age;
        function SetAge($age) {..... description......}
        function SetName($name) {..... description......}
        function ShowData() {..... description......}
}

Table of Contents

  • 1 VISIBILITY FUNCTION IN CLASS OBJECT ORIENTED PROGRAMMING
    • 1.1 USING POINTER $this
    • 1.2 CREATE NEW OBJECT
    • 1.3 HOW TO ACCESS THE CLASS FUNCTION

VISIBILITY FUNCTION IN CLASS OBJECT ORIENTED PROGRAMMING

Classes are created by default which has the data and functions that are global or public, where all objects of other classes can access these functions. In PHP, there are 5 important keywords ie

1. public : If you use this keyword then, data or functions can be accessed by the object from the outside
2. private : Allow data and functions accessible from within the class itself. Data or function with the keyword private inaccessible other classes or subclasses
3. protected : Allow data or functions, accessed by the class itself or subclasses
4. final : Used to prevent the function from the parent can not be redefined in the derived function.
5. abstract : This function must be declared back in derivatives function, which is inverse with final

USING POINTER $this

The important pointer to note is $this . $this should be used if you will access / use existing data member in the class. If you do not use $this at the time of accessing the data member, it will cause a fatal error.
$this works to reference the object caller, so the data or functions that will be called from the new data type will be returned to the caller object precisely, consider the following example

1
2
3
4
5
6
7
8
9
10
11
12
class Cat
{
private
           $name;
           $age;
        public function SetAge($age) {
             $this->age = $age
        }
        public function GetAge(){
            return $this->age;
        }
}

Important :
If you accessing data such as data $age, it can use the keyword $this with $this->age. Note the $age residing in the variable, when accessed premises $this keyword, then the $ sign must be removed on the variable name

1
2
$this->age // valid
$this->$age // wrong

CREATE NEW OBJECT

After creating a new data type, then you can create an object based on that type. To create an object of a new data type, or class then use the new keyword followed by the name of the class referred. Consider the following script

1
2
$pussy = new Cat();
$pussy = new Cat(12, "garfield")

The first script is used to create objects pussy with data type Cat (), while the second line we create objects pussy with data type Cat () followed by the parameters age and name.

HOW TO ACCESS THE CLASS FUNCTION

After creating objects using a new data type that we can access all the components that are in the class. To access the functions that exist in the new data type then use the object name followed by the name of the function using the arrow.

1
2
3
$pussy = new Cat()
$pussy->SetAge('12');
echo $pussy->GetAge();

How ? easy right ? article above is the basic foundation of making a php class using object oriented programming. Before you learn this session, you can read my article before about Concept Object Oriented Programming PHP for beginners

Another PHP Related Post :

  • How To Replace String With Another String In PHP
  • Login Page – Tutorial CRUD Client and API Server Using JQuery And Lumen Part 2
  • Tutorial CRUD Client and API Server Using JQuery And Lumen Part 1
  • How To Solve Problems Illegal mix of collations (latin1_swedish_ci,IMPLICIT) In Laravel
  • How To Resolve No ‘Access-Control-Allow-Origin’ Header In Lumen
  • How To Create Custom Class In Laravel 5.5 For Beginners

Avatar for Sigit Prasetya Nugroho

About Sigit Prasetya Nugroho

This site is a personal Blog of Sigit Prasetya Nugroho, a Desktop developer and freelance web developer working in PHP, MySQL, WordPress.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Welcome to my Home,

Avatar for Sigit Prasetya NugrohoThis site is a personal Blog of Sigit Prasetya Nugroho, a Desktop developer and freelance web developer working in PHP, MySQL, WordPress.



Popular Articles

Checked checkbox AdminLTE Bootstrap in Jquery

November 4, 2014 By Sigit Prasetya Nugroho 7 Comments

Simple create date format validation with jqueryUI

December 21, 2014 By Sigit Prasetya Nugroho Leave a Comment

Create Simple Progress Bar for Fake Online Generator with Jquery

January 10, 2015 By Sigit Prasetya Nugroho Leave a Comment

22+ Coolest Free Jquery Plugin For Premium Theme

October 3, 2015 By Sigit Prasetya Nugroho Leave a Comment

Easy Build Your Anti Copy Paste Plugin

October 6, 2015 By Sigit Prasetya Nugroho Leave a Comment

Popular Tags

adminlte (15) adsense (13) adsense tips (4) affiliate amazon (13) amazon (12) Android (8) angular (16) angular 4 (12) angular 5 (4) asin grabber (3) Bootstrap (27) codeigniter (5) create wordpress theme (5) crud (8) css (6) free wordpress theme (7) google adsense (4) imacros (4) increase traffic (6) jquery (34) laravel (10) laravel 5 (5) learn android (5) modal dialog (5) mysql (6) nodeJs (4) optimize seo (4) pdo (6) php (30) plugin (53) pos (7) Publisher Tips (5) react (3) Reactjs (7) SEO (37) theme (17) tutorial angular (5) tutorial angular 4 (6) tutorial javascript (10) tutorial javascript beginners (4) twitter (3) widget (3) wordpress (18) wordpress plugin (13) XMLRPC (5)




  • About
  • Contact Us
  • Disclaimer
  • Privacy Policy
  • Terms and Conditions

©2021 Seegatesite.com