search
HomeBackend DevelopmentPHP TutorialShare the differences between the three data types in the php5 class, php5 data types_PHP tutorial

Share the differences between the three data types in the php5 category, php5 data types

public: public type
In the subclass, you can call the public type method or attribute through self::var. You can call the method in the parent class through parent::method.
In an instance, you can call public type methods or properties through $obj->var

protected: protected type
In subclasses, you can use self::var to call protected type methods or attributes. You can use parent::method to call methods in parent classes
Methods or properties of protected types cannot be called through $obj->var in an instance

private: private type
The attributes or methods of this type can only be used in this class. Private type attributes and methods cannot be called in instances of this class, subclasses, or instances of subclasses


2.The difference between self and parent
a). These two objects are commonly used in subclasses. The main difference between them is that self can call public or protected properties in the parent class, but parent cannot call

b).self:: It represents the static members (methods and properties) of the current class. Unlike $this, $this refers to the current object

Attached code:

<&#63;php
/**
 * parent 只能调用父类中的公有或受保护的方法,不能调用父类中的属性
 * self  可以调用父类中除私有类型的方法和属性外的所有数据
 */
class User{
 public $name;
 private $passwd;
 protected $email; 
 public function __construct(){
  //print __CLASS__." ";
  $this->name= 'simple';
  $this->passwd='123456';
  $this->email = 'bjbs_270@163.com';
 } 
 public function show(){
  print "good ";
 } 
 public function inUserClassPublic() {
  print __CLASS__.'::'.__FUNCTION__." ";
 } 
 protected function inUserClassProtected(){
  print __CLASS__.'::'.__FUNCTION__." ";
 } 
 private function inUserClassPrivate(){
  print __CLASS__.'::'.__FUNCTION__." ";
 }
}

class simpleUser extends User { 
 public function __construct(){  
  //print __CLASS__." ";
  parent::__construct();
 }
 
 public function show(){
  print $this->name."//public ";  
  print $this->passwd."//private ";
  print $this->email."//protected ";
 }
 
 public function inSimpleUserClassPublic() {
  print __CLASS__.'::'.__FUNCTION__." ";
 }
 
 protected function inSimpleUserClassProtected(){
  print __CLASS__.'::'.__FUNCTION__." ";
 }
 
 private function inSimpleUserClassPrivate() {
  print __CLASS__.'::'.__FUNCTION__." ";
 }
}

class adminUser extends simpleUser {
 protected $admin_user;
 public function __construct(){
  //print __CLASS__." ";
  parent::__construct();
 }
 
 public function inAdminUserClassPublic(){
  print __CLASS__.'::'.__FUNCTION__." ";
 }
 
 protected function inAdminUserClassProtected(){
  print __CLASS__.'::'.__FUNCTION__." ";
 }
 
 private function inAdminUserClassPrivate(){
  print __CLASS__.'::'.__FUNCTION__." ";
 }
}

class administrator extends adminUser {
 public function __construct(){  
  parent::__construct();
 }
}

/**
 * 在类的实例中 只有公有属性和方法才可以通过实例化来调用
 */
$s = new administrator();
print '-------------------';
$s->show();
&#63;>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/947911.htmlTechArticleShare the differences between the three data types in the php5 class. The php5 data type public: Public types can be used in subclasses Calling public type methods or properties through self::var can be done through parent...
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 Dependency Injection Container: A Quick StartPHP Dependency Injection Container: A Quick StartMay 13, 2025 am 12:11 AM

APHPDependencyInjectionContainerisatoolthatmanagesclassdependencies,enhancingcodemodularity,testability,andmaintainability.Itactsasacentralhubforcreatingandinjectingdependencies,thusreducingtightcouplingandeasingunittesting.

Dependency Injection vs. Service Locator in PHPDependency Injection vs. Service Locator in PHPMay 13, 2025 am 12:10 AM

Select DependencyInjection (DI) for large applications, ServiceLocator is suitable for small projects or prototypes. 1) DI improves the testability and modularity of the code through constructor injection. 2) ServiceLocator obtains services through center registration, which is convenient but may lead to an increase in code coupling.

PHP performance optimization strategies.PHP performance optimization strategies.May 13, 2025 am 12:06 AM

PHPapplicationscanbeoptimizedforspeedandefficiencyby:1)enablingopcacheinphp.ini,2)usingpreparedstatementswithPDOfordatabasequeries,3)replacingloopswitharray_filterandarray_mapfordataprocessing,4)configuringNginxasareverseproxy,5)implementingcachingwi

PHP Email Validation: Ensuring Emails Are Sent CorrectlyPHP Email Validation: Ensuring Emails Are Sent CorrectlyMay 13, 2025 am 12:06 AM

PHPemailvalidationinvolvesthreesteps:1)Formatvalidationusingregularexpressionstochecktheemailformat;2)DNSvalidationtoensurethedomainhasavalidMXrecord;3)SMTPvalidation,themostthoroughmethod,whichchecksifthemailboxexistsbyconnectingtotheSMTPserver.Impl

How to make PHP applications fasterHow to make PHP applications fasterMay 12, 2025 am 12:12 AM

TomakePHPapplicationsfaster,followthesesteps:1)UseOpcodeCachinglikeOPcachetostoreprecompiledscriptbytecode.2)MinimizeDatabaseQueriesbyusingquerycachingandefficientindexing.3)LeveragePHP7 Featuresforbettercodeefficiency.4)ImplementCachingStrategiessuc

PHP Performance Optimization Checklist: Improve Speed NowPHP Performance Optimization Checklist: Improve Speed NowMay 12, 2025 am 12:07 AM

ToimprovePHPapplicationspeed,followthesesteps:1)EnableopcodecachingwithAPCutoreducescriptexecutiontime.2)ImplementdatabasequerycachingusingPDOtominimizedatabasehits.3)UseHTTP/2tomultiplexrequestsandreduceconnectionoverhead.4)Limitsessionusagebyclosin

PHP Dependency Injection: Improve Code TestabilityPHP Dependency Injection: Improve Code TestabilityMay 12, 2025 am 12:03 AM

Dependency injection (DI) significantly improves the testability of PHP code by explicitly transitive dependencies. 1) DI decoupling classes and specific implementations make testing and maintenance more flexible. 2) Among the three types, the constructor injects explicit expression dependencies to keep the state consistent. 3) Use DI containers to manage complex dependencies to improve code quality and development efficiency.

PHP Performance Optimization: Database Query OptimizationPHP Performance Optimization: Database Query OptimizationMay 12, 2025 am 12:02 AM

DatabasequeryoptimizationinPHPinvolvesseveralstrategiestoenhanceperformance.1)Selectonlynecessarycolumnstoreducedatatransfer.2)Useindexingtospeedupdataretrieval.3)Implementquerycachingtostoreresultsoffrequentqueries.4)Utilizepreparedstatementsforeffi

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

Video Face Swap

Video Face Swap

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

Hot Article

Hot Tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

EditPlus Chinese cracked version

EditPlus Chinese cracked version

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