search
HomeBackend DevelopmentPHP TutorialDetailed explanation of basic object-oriented concepts and examples of PHP introductory tutorial

This article mainly introduces the basic concepts of object-oriented in PHP introductory tutorial, and briefly analyzes the definition of classes, object creation, constructors, member variables, member methods, etc. involved in PHP object-oriented in the form of examples. What is needed Friends can refer to

Demo1.php

<?php
  //怎样去创建一个类 格式:修饰符 class 类名{}
  //我们去创建一个电脑的类,这类可以创建出对象(生产出电脑)
  class Computer { //类名第一个字母大写
  }
  //创建一台电脑出来,也就是对象的声明
  //格式:变量 = new 类名();
  //new Compuer() 表示实例化的过程(意思是创建一个对象)
  //$compuer1 = new Compuer() 这个过程就是把实例化对象的地址给 $compuer1
  //$compuer1 我们就可以称作为对象的应用
  $computer1 = new Computer();  //这是我们创建的第一台电脑
  $computer2 = $computer1;  //这是我们创建的第二台电脑
  var_dump($computer1);
  echo &#39;<br/>&#39;;
  var_dump($computer1);
?>

Demo2.php

<?php
  header(&#39;Content-Type:text/html; charset=utf-8;&#39;);
// class Computer {
//   //字段成员的声明格式:修饰符 变量名 [=xxx];
//   public $_name = &#39;联想&#39;;  //public 表示共有,类外可以访问
//   public $_model = &#39;i7&#39;;
// }
// //创建一个对象,生产出一台电脑 -> 表示指向
// $computer1 = new Computer();
// echo $computer1 -> _name;
// $computer1 -> _name = &#39;dell&#39;;
// echo $computer1 -> _name;
  class Computer {
    //字段成员的声明格式:修饰符 变量名 [=xxx];
    public $_name; //public 表示共有,类外可以访问
    public $_model;
  }
  //创建一个对象,生产出一台电脑 -> 表示指向
  $computer1 = new Computer();
  //给成员字段赋值
  $computer1 -> _name = &#39;联想&#39;;
  //取值
  echo $computer1 -> _name;
?>

Demo3.php

<?php
  header(&#39;Content-Type:text/html; charset=utf-8;&#39;);
  class Computer {
    public $_name; //public 表示共有,类外可以访问
    public $_model;
    //创建方法的格式:修饰符 function 方法名(){}
    //如果不加修饰符,默认就是public
    function _run(){
      echo &#39;我是运行的方法&#39;;
    }
  }
  //创建一个对象,生产出一台电脑 -> 表示指向
  $computer1 = new Computer();
  $computer1 -> _run();
?>

Demo4.php

<?php
  header(&#39;Content-Type:text/html; charset=utf-8;&#39;);
// class Computer {
//   //字段成员的声明格式:修饰符 变量名 [=xxx];
//   public $_name = &#39;联想&#39;;  //public 表示共有,类外可以访问
//   public $_model = &#39;i7&#39;;
// }
// //创建一个对象,生产出一台电脑 -> 表示指向
// $computer1 = new Computer();
// echo $computer1 -> _name;
// $computer1 -> _name = &#39;dell&#39;;
// echo $computer1 -> _name;
  class Computer {
    //字段成员的声明格式:修饰符 变量名 [=xxx];
    public $_name; //public 表示共有,类外可以访问
    public $_model;
  }
  //创建一个对象,生产出一台电脑 -> 表示指向
  $computer1 = new Computer();
  //给成员字段赋值
  $computer1 -> _name = &#39;联想&#39;;
  //取值
  //echo $computer1 -> _name;
  $computer2 = $computer1;
  echo $computer2 -> _name;
?>

Demo5.php

<?php
  header(&#39;Content-Type:text/html; charset=utf-8;&#39;);
  class Computer {
    public $_name; //public 表示共有,类外可以访问
    public $_model;
    //创建方法的格式:修饰符 function 方法名(){}
    //如果不加修饰符,默认就是public
    function _run($_who){
      echo $_who.&#39;是运行的方法&#39;;
    }
  }
  //创建一个对象,生产出一台电脑 -> 表示指向
  $computer1 = new Computer();
  $computer1 -> _run(&#39;一站式建网站&#39;);
?>

Demo6.php

<?php
  header(&#39;Content-Type:text/html; charset=utf-8;&#39;);
  class Computer {
    //创建一个构造方法
    public function Computer(){
      echo &#39;我是构造方法&#39;;
    }
  }
  //只要实例化,就可以运行构造方法
  //$computer = new Computer();
  new Computer();
?>

Demo7.php

<?php
  header(&#39;Content-Type:text/html; charset=utf-8;&#39;);
  class Computer {
    //创建一个构造方法
    public function __construct(){
      echo &#39;我是比较先进的构造方法&#39;;
    }
  }
  //只要实例化,就可以运行构造方法
  //$computer = new Computer();
  new Computer();
?>

Demo8.php

<?php
  header(&#39;Content-Type:text/html; charset=utf-8;&#39;);
  class Computer {
    //创建一个构造方法
    public function __construct(){
      echo &#39;我是比较先进的构造方法&#39;;
    }
    //析构方法
    public function __destruct(){
      echo &#39;我是析构方法&#39;;
    }
    //普通方法
    public function _run(){
      echo &#39;我是普通方法&#39;;
    }
  }
  //只要实例化,就可以运行构造方法
  $computer = new Computer();
  $computer -> _run();
?>

##Summary: That’s it The entire content of this article is hoped to be helpful to everyone's study.

Related recommendations:

PHP method to implement two-dimensional array sorting by a certain column_phpTips

PHP method to implement batch acquisition of all fixed seed links in web pages

PHP method to implement two-dimensional array sorting by a certain column_phpTips

The above is the detailed content of Detailed explanation of basic object-oriented concepts and examples of PHP introductory tutorial. For more information, please follow other related articles on the PHP Chinese website!

Statement
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
php怎么把负数转为正整数php怎么把负数转为正整数Apr 19, 2022 pm 08:59 PM

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

PHP实现框架:CakePHP入门教程PHP实现框架:CakePHP入门教程Jun 18, 2023 am 09:04 AM

随着互联网技术的不断发展,Web开发技术也在不断更新迭代。PHP作为一种开源的编程语言,在Web开发中拥有广泛的应用。而PHP框架作为PHP开发中常用的工具之一,能够提高开发效率和代码质量。本文将为大家介绍一个PHP框架——CakePHP,并提供一些简单入门的教程。一、什么是CakePHP?CakePHP是一个基于MVC(Model-View-Control

初学者指南:从零开始,逐步学习MyBatis初学者指南:从零开始,逐步学习MyBatisFeb 19, 2024 am 11:05 AM

简明易懂的MyBatis入门教程:一步一步编写你的第一个程序MyBatis是一种流行的Java持久层框架,它简化了与数据库交互的过程。本教程将为您介绍如何使用MyBatis创建和执行简单的数据库操作。第一步:环境搭建首先,确保您的Java开发环境已经安装好。然后,下载MyBatis的最新版本,并将其添加到您的Java项目中。您可以从MyBatis的官方网站下

php怎么除以100保留两位小数php怎么除以100保留两位小数Apr 22, 2022 pm 06:23 PM

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

php怎么根据年月日判断是一年的第几天php怎么根据年月日判断是一年的第几天Apr 22, 2022 pm 05:02 PM

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

php怎么判断有没有小数点php怎么判断有没有小数点Apr 20, 2022 pm 08:12 PM

php判断有没有小数点的方法:1、使用“strpos(数字字符串,'.')”语法,如果返回小数点在字符串中第一次出现的位置,则有小数点;2、使用“strrpos(数字字符串,'.')”语句,如果返回小数点在字符串中最后一次出现的位置,则有。

php怎么设置implode没有分隔符php怎么设置implode没有分隔符Apr 18, 2022 pm 05:39 PM

在PHP中,可以利用implode()函数的第一个参数来设置没有分隔符,该函数的第一个参数用于规定数组元素之间放置的内容,默认是空字符串,也可将第一个参数设置为空,语法为“implode(数组)”或者“implode("",数组)”。

php怎么查找字符串是第几位php怎么查找字符串是第几位Apr 22, 2022 pm 06:48 PM

查找方法:1、用strpos(),语法“strpos("字符串值","查找子串")+1”;2、用stripos(),语法“strpos("字符串值","查找子串")+1”。因为字符串是从0开始计数的,因此两个函数获取的位置需要进行加1处理。

See all articles

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Atom editor mac version download

Atom editor mac version download

The most popular open source editor