How to write php objects and arrays respectively

王林
Release: 2023-05-19 12:52:08
Original
582 people have browsed it

PHP is a web development language that provides a variety of data types for developers to use. Objects and arrays are the two most commonly used data types in PHP. This article will introduce the differences between objects and arrays in PHP, as well as their definition and use.

1. Definition and usage of objects

Object is a composite data type in PHP, which can store multiple data (properties) and functions (methods). Objects are usually associated with classes, which are templates that define the properties and methods of an object. Developers can define an object template in a class to declare the object's properties and methods.

The first step in defining an object is to create a class. You can use the class keyword:

class User {
    // 定义属性
    private $name;
    private $age;
    // 定义方法
    public function setName($name) {
        $this->name = $name;
    }
    public function getName() {
        return $this->name;
    }
    public function setAge($age) {
        $this->age = $age;
    }
    public function getAge() {
        return $this->age;
    }
}
Copy after login

The above code defines a class named User, which has two private properties $name and $age, and four public methods. The setName() and setAge() methods are used to set the values ​​of the $name and $age attributes, and the getName() and getAge() methods are used to obtain the values ​​of the $name and $age attributes.

To create an object, you need to use the new keyword:

$user = new User();
Copy after login

You can now use the object $user to access the properties and methods in the class, for example:

$user->setName('张三');
$user->setAge(18);

echo '名字:' . $user->getName() . '
'; echo '年龄:' . $user->getAge();
Copy after login

The above code will The $name attribute of the $user object is set to "Zhang San", the $age attribute of the $user object is set to "18", and the getName() and getAge() methods are used to obtain the attribute values ​​of the $user object respectively (the output result is " Name: Zhang San" and "Age: 18").

2. Definition and usage of arrays

Array is another common data type in PHP, which is used to store a set of related data. The data in the array can be strings, numbers, and other data types.

Defining an array is very simple, just use the array() function:

$array = array('张三', '李四', '王五');
Copy after login

The above code defines an array named $array, which contains three elements: "Zhang San "," Li Si" and "Wang Wu".

You can use array subscripts to access array elements:

echo '第一个元素是:' . $array[0];
Copy after login

The above code will output "The first element is: Zhang San" because the array subscripts start from 0.

To create an associative array, you can use the following method:

$assoc_array = array('name' => '张三', 'age' => 18);
Copy after login

The above code defines an associative array named $assoc_array. The array has two elements, which are key-value pairs: name => Zhang Sanhe age => 18.

You can use key names to access associative array elements:

echo '名字是:' . $assoc_array['name'];
Copy after login

The above code will output "The name is: Zhang San" because the key of name in the key-value pair is "Zhang San".

3. The difference between objects and arrays

Objects and arrays are very important data types in PHP, but there are many differences between them:

  1. Objects are created by classes, arrays are not.
  2. The data items in an object are usually properties and methods, while the data items in an array are just elements.
  3. Arrays can store a variety of data types, such as strings, numbers, and Boolean values. Objects are typically "custom" data types that allow developers to define properties and methods based on application needs.
  4. Objects have a life cycle and can be destroyed after instantiation. Arrays have no such concept.

The above is the detailed content of How to write php objects and arrays respectively. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!