Introduction to PHP Object usage

Source: Internet
Author: User

This tutorial is about object-oriented programming (OOP) with PHP. After finishing this tutorial, you should have the basic tools necessary to start navigating the world's objects. Throughout, we will draw on an example, while key terms and nuances are introduced in PHP object-oriented. In addition, a period of time will be used to show how to put object-oriented php with MySQL.

On the menu are:

Create our first object
class variables and visibility
Construction method
Using arrays to speed up coding
Law interaction
Object-oriented PHP integration and MySQL
Introducing Inheritance
Word segmentation
Now let's get started!
Create our first object
It is not surprising that object-oriented programming revolves around objects. Although the idea of objects may be foreign to you in terms of coding, understand objects and why we use them should not take long. In short, our non programming world is made up of objects. Computers, fish, clouds, people, cars are objects. The properties of an object, such as color, size, name, and speed. Objects can also be made by other objects: car doors, door handles, plastic levers for processing, and so on. Objects are all around us. For this reason, many programmers find object-oriented programming easier to understand.

So, how does an object turn into code? If we build a website for users, we will create one or more user objects. Create an object usage lesson. The variables and functions associated with the course group. Variables hold properties of objects such as color, size, and speed. Perform functions like actions set variable values or open a file. In this example, we can create user objects and user classes. You can put the user level as template for any user object.

Creating an object is known as an instance (creating an instance). Let's instantiate a user object and the corresponding user class for the code:

/* Create new Object * *
$kirupa = new User;

/* class to create objects with/*
Class User {
}

Note: As the progress of this tutorial, the code will be removed so that you do not lose and can focus on the task at hand. Always keep or re-add code from previous examples.

Now, this user cannot do anything, without any attributes. In the class, we use the method (also known as function) and the variables to let our objects function and performance. Let's redo our first example so that our users can have a name:

* Create a new Object */
$kirupa = new user;

/* Call Object Methods */
$kirupa->setname (' Kirupa ');
Echo $kirupa->getname (). ' <br/> ';

Class User {
 private $name
 
 function setname ($val) {
   $this->name = $val;
  return;
 }
 
 function getName () {
  return $this->name;
 }
}

As you can see, the method is to use the feature to declare [methodname] format, which is standard in PHP. This simple example shows how we can apply the name of the object and then access the name. Continue to learn about how we handle variables and methods of course.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.