Home php教程 php手册 php学习笔记 数组的常用函数_php入门_脚本之家

php学习笔记 数组的常用函数_php入门_脚本之家

Jun 06, 2016 pm 08:37 PM
Commonly used functions array

只要一个变量,需要在多个方法使用,就将这个变量声明为成员属性,可以直接在这个对象中的所有方法中使用 成员属性,相当于这个对象中的全局变量

代码如下:
/*
* 封装性:面向对象三大特性之一
*
* 1.就是把对象的成员(属性,方法)结合成一个独立的相同单位,并尽可能隐藏对象的内部细节
* 访问权限修饰符 public protected private
* private:私有的,用这个关键字修饰的成员,只能在对象内部访问(只有用$this访问)
*
* 属性可以封装:
* 只要一个变量,需要在多个方法使用,就将这个变量声明为成员属性,可以直接在这个对象中的所有方法中使用
*
* 成员属性,相当于这个对象中的全局变量
*
* 成员属性都会在方法中使用,成员属性值的变化其实就是在改变方法的执行行为,也就是改变了对象的功能
*
* 成员属性的值如果不正常,方法执行的功能叶就不正常
*
* 作用:不需要在对象外部改变或读取它的值
* 1.封装
* 再提供一个公有的方法(经过方法对对象成员属性进行赋值和取值就可以控制)
*
*
* 方法也可以封装
*
* 作用:
* 1.使用private修饰使其只能在内部使用
*
* 2.一个类中有100个方法,封装了95个(为另外5个服务的方法),只有5个方法可以使用
*
* 100个成员属性,都让取值,不可以改值;或者只可改值,不可获取值//此种情况下使用以下方法比较方便
* 和封装有关的魔术方法:
*
* __set();//是直接设置[私有的]成员属性值时,自动调用的方法
* __get();//是直接获取[私有的]成员属性值时,自动调用的方法
* __isset();//是直接使用isset()查看对象中私有属性是否存在时,自动调用这个方法
* __unset();//是直接使用unset()删除对象中私有属性时,自动调用的方法
*
*
*
*
*
*
*
*/
class Person{
//x封装成员属性,不需要在对象外部改变
private $name;
private $age;
private $sex;
private __unset($proName){
unset($this->$proName);
}
//是直接查看对象中私有属性是否存在时,自动调用这个方法
//__isset($proName)的使用,$proName代表属性名
private function __isset($proName){
return isset($this->$proName);//isset()返回是否存在
}
function __construct($name,$age,$sex){
$this->name=$name;
$this->age=$age;
$this->sex=$sex;
}
//当获取私有的成员属性时,自动调用此方法
private function __get($proName)
{
//控制所获取的值
if($proName=="age"){
if($this-age>40)
return $this->age-10;
}
return $this->$proName;
}
//当设置私有的成员属性时,自动调用此方法
private function __set($proName,$proValue){
//$proName表示成员属性名,$proValue表示成员属性值
//控制设置范围
if($proName=="age"){
if($proValue > 100 || $proValuereturn;
}
$this->$proName=$proValue;
}
//提供公有方法来设置成员属性的值
function setAge($age){
//控制年龄范围,增加安全性
if($age > 100 || $age return;
$this->age=$age;
}
//提供公有方法来获取成员属性的值
function getAge(){
//控制获取年龄的范围
if($this->age return $this->age;
else if($this->agereturn $this->age-5;
else if($this->agereturn $this->age;
else
return $this->age-15;
提供公有方法来 }
function say(){
echo "我的名字:{$this->name},我的年龄:{$this->age},我的姓别:{$this->sex}
";
//访问封装过的 run()方法
$this-run();
}
private function run(){
echo '111111111111
'
}
function eat(){
}
//析构方法
function __destruct(){
}
}
$p1=new Person("zhangsan",25,"男");
$p2=new Person;
$p3=new Person;
//$p1->age=-50;//因为年龄在外部随意访问,所以成员属性药封装,才有安全性。
$p1->setAge(30);//通过方法来设置成员属性德值
$p1->getAge();//通过方法来获取成员属性德值
//通过添加家魔术方法__set($proName,$proValue) __get($proName),就可以直接调用成员属性了
$p1->say();//可以调用
$p1->run();//私有的方法不能直接调用
//删除$p1里面的name
unset($p1->name);
//判断name是否存在
if(isset($p1->name)){
echo "存在
";
}else{
echo "没有这个成员
";
}
?>

作者:代号极光
出处:http://zizhuyuan.cnblogs.com
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to remove duplicate elements from PHP array using foreach loop? How to remove duplicate elements from PHP array using foreach loop? Apr 27, 2024 am 11:33 AM

The method of using a foreach loop to remove duplicate elements from a PHP array is as follows: traverse the array, and if the element already exists and the current position is not the first occurrence, delete it. For example, if there are duplicate records in the database query results, you can use this method to remove them and obtain results without duplicate records.

The Art of PHP Array Deep Copy: Using Different Methods to Achieve a Perfect Copy The Art of PHP Array Deep Copy: Using Different Methods to Achieve a Perfect Copy May 01, 2024 pm 12:30 PM

Methods for deep copying arrays in PHP include: JSON encoding and decoding using json_decode and json_encode. Use array_map and clone to make deep copies of keys and values. Use serialize and unserialize for serialization and deserialization.

PHP array key value flipping: Comparative performance analysis of different methods PHP array key value flipping: Comparative performance analysis of different methods May 03, 2024 pm 09:03 PM

The performance comparison of PHP array key value flipping methods shows that the array_flip() function performs better than the for loop in large arrays (more than 1 million elements) and takes less time. The for loop method of manually flipping key values ​​takes a relatively long time.

PHP array multi-dimensional sorting practice: from simple to complex scenarios PHP array multi-dimensional sorting practice: from simple to complex scenarios Apr 29, 2024 pm 09:12 PM

Multidimensional array sorting can be divided into single column sorting and nested sorting. Single column sorting can use the array_multisort() function to sort by columns; nested sorting requires a recursive function to traverse the array and sort it. Practical cases include sorting by product name and compound sorting by sales volume and price.

Best Practices for Deep Copying PHP Arrays: Discover Efficient Methods Best Practices for Deep Copying PHP Arrays: Discover Efficient Methods Apr 30, 2024 pm 03:42 PM

The best practice for performing an array deep copy in PHP is to use json_decode(json_encode($arr)) to convert the array to a JSON string and then convert it back to an array. Use unserialize(serialize($arr)) to serialize the array to a string and then deserialize it to a new array. Use the RecursiveIteratorIterator to recursively traverse multidimensional arrays.

Application of PHP array grouping function in data sorting Application of PHP array grouping function in data sorting May 04, 2024 pm 01:03 PM

PHP's array_group_by function can group elements in an array based on keys or closure functions, returning an associative array where the key is the group name and the value is an array of elements belonging to the group.

How to sort values ​​in an array by size in PHP How to sort values ​​in an array by size in PHP Mar 22, 2024 pm 05:24 PM

PHP is a commonly used server-side scripting language widely used in website development and data processing fields. In PHP, it is a very common requirement to sort the values ​​in an array by size. By using the built-in sort function, you can easily sort arrays. The following will introduce how to use PHP to sort the values ​​in an array by size, with specific code examples: 1. Sort the values ​​in the array in ascending order:

The role of PHP array grouping function in finding duplicate elements The role of PHP array grouping function in finding duplicate elements May 05, 2024 am 09:21 AM

PHP's array_group() function can be used to group an array by a specified key to find duplicate elements. This function works through the following steps: Use key_callback to specify the grouping key. Optionally use value_callback to determine grouping values. Count grouped elements and identify duplicates. Therefore, the array_group() function is very useful for finding and processing duplicate elements.

See all articles