Php Object Example


A class is a template for objects, and an object is an instance of class.

Bạn đang xem: Php object example

OOP.. Case

Let"s assume we have a class named Fruit. A Fruit can have sầu properties lượt thích name, color, weight, etc. We can define variables lượt thích $name, $color, & $weight khổng lồ hold the values of these properties.

When the individual objects (táo bị cắn dở, banamãng cầu, etc.) are created, they inherit all the properties & behaviors from the class, but each object will have different values for the properties.

Xem thêm: Bí Quyết Sống Khỏe Của Người Nhật Có Gì Đặc Biệt, Bí Quyết Sống Khỏe Mạnh Như Người Nhật Bản

Define a Class

A class is defined by using the class từ khoá, followed by the name of the class & a pair of curly braces (?>

Below we declare a class named Fruit consisting of two properties ($name and $color) and two methods set_name() and get_name() for setting và getting the $name property:


Define Objects

Classes are nothing without objects! We can create multiple objects from a class. Each object has all the properties và methods defined in the class, but they will have different property values.

Objects of a class is created using the new keywords.

In the example below, $apple và $banana are instances of the class Fruit:



set_name("Apple");$banana->set_name("Banana"); emang lại $apple->get_name();emang lại "
"; echo $banana->get_name();?>

In the example below, we add two more methods khổng lồ class Fruit, for setting and getting the $color property:



set_name("Apple"); $apple->set_color("Red");emang lại "Name: " . $apple->get_name();emang đến "
"; emang lại "Color: " . $apple->get_color();?>

PHPhường - The $this Keyword

The $this từ khoá refers lớn the current object, và is only available inside methods.

Look at the following example:




So, where can we change the value of the $name property? There are two ways:

1. Inside the class (by adding a set_name() method & use $this):


PHPhường. - instanceof

You can use the instanceof keywords to lớn check if an object belongs to lớn a specific class:


×

Report Error

If you want to lớn report an error, or if you want lớn make a suggestion, vì chưng not hesitate khổng lồ sover us an e-mail:

help


Top Tutorials HTML Tutorial CSS Tutorial JavaScript Tutorial How To Tutorial Squốc lộ Tutorial Pyhẹp Tutorial W3.CSS Tutorial Bootstrap Tutorial PHPhường Tutorial Java Tutorial C++ Tutorial jQuery Tutorial
Top References HTML Reference CSS Reference JavaScript Reference Squốc lộ Reference Pykhông lớn Reference W3.CSS Reference Bootstrap Reference PHPhường Reference HTML Colors Java Reference Angular Reference jQuery Reference
Top Examples HTML Examples CSS Examples JavaScript Examples How To Examples Squốc lộ Examples Pyhẹp Examples W3.CSS Examples Bootstrap Examples PHPhường Examples Java Examples XML Examples jQuery Examples
Web Courses HTML Course CSS Course JavaScript Course Front End Course SQL Course Pybé Course PHP Course jQuery Course Java Course C++ Course C# Course XML Course Get Certified »
obatambeienwasirherbal.com is optimized for learning và training. Examples might be simplified lớn improve sầu reading & learning. Tutorials, references, & examples are constantly reviewed to lớn avoid errors, but we cannot warrant full correctness of all content. While using obatambeienwasirherbal.com, you agree lớn have read và accepted our terms of use, cookie and privacy policy. Copyright 1999-2021 by Refsnes Data. All Rights Reserved. obatambeienwasirherbal.com is Powered by W3.CSS.

*