PHP继承小问题
自己写了一点测试继承关系的代码,如下:
class A
{
public $pub_A;
private $pri_A;
public function pub()
{
$this->pub_A = 'It is public of A!';
echo "
".$this->pub_A."
";}
protected function pri()
{
$this->pri_A = 'It is private of A!';
echo "
".$this->pri_A."
";}
}
class B extends A
{
public $pub_B;
private $pri_B;
function __construct()
{
echo "
".$this->pri()."
";$this->pri_A = 10;
echo "
".$this->pri_A."
";}
}
$b = new B;
?>
重点我标了出来,在父类A中,$pri_A是private的,应该不能被子类B继承,可为什么下面可以对pri_A赋值并且显示出赋值后的数?
------解决方案--------------------
private:方法或者属性只能从类的一个成员中访问到,而且无法从继承类的成员中访问到。
经过private便签标记的方法或者属性可以在继承类中重新定义。
每一个类只能看到它自己定义的私有方法。
------解决方案--------------------
子类可以通过父类的公有方法去访问父类的属性
------解决方案--------------------
我测试了下代码,如果在A中给$pri_A="aaa",在B中不赋值的话将不能得到值,在B中赋值之后得到赋值后的值,可见在子类中是可以对父类的私有属性重新定义的。
------解决方案--------------------
问题不是子类访问了 父类的 私有成员
而是 子类调用了$this->pri()方法
这个方法是继承父类的
但是父类的这个方法调用了父类的私有成员
------解决方案--------------------
是的,私有属性不被继承!
这一点你可以通过这样看到
function __construct()
{
echo "
".$this->pri()."
";echo isset($this->pri_A) ? 'yes' : 'no';//这里将输出 no
$this->pri_A = 10;
echo "
".$this->pri_A."
";}
pri() 方法是父类的,自然可以访问其私有属性
当你 $this->pri_A = 10; 后,将在对象 $b 中创建名为 pri_A 的公共属性(public)
这一点你可以这样看到
print_r($b);
输出:
B Object
(
[pub_B] =>
[pri_B:private] =>
[pub_A] =>
[pri_A:private] => It is private of A!
[pri_A] => 10
)

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

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.

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

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

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

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

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.

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


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Dreamweaver Mac version
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.
