How to Use parent variables for subclass

Source: Internet
Author: User
Subclass uses the parent class variable classA {var $ authKey = '000000';} classBextendsA {& nbsp ;__ construct () {& nbsp; echoparent: authKey ;}} the error "Undefinedclassconstant 'authkey'" is returned. the error message indicates that the undefined subclass uses the parent class variable.
Class {
Var $ authKey = '20140901 ';

}

Class B extends {
_ Construct (){
Echo parent: authKey;
}
}
Error: Undefined class constant 'authkey'

------ Solution --------------------
The error message indicates an undefined constant. You are missing $
But $ authKey is not a static variable, so you cannot call it so statically.
Correct practice
PHP code
Class A {// use var to declare the variable public $ authKey = '000000 ';} class B extends A {// B will have all non-private members of A public function _ construct () {echo $ this-> authKey ;}}
------ Solution --------------------
Var is a version earlier than php4 and is omitted later.
------ Solution --------------------
Var should be taken as appropriate! Some earlier versions support
------ Solution --------------------
Var is used in php 4.x, and 5 + for backward compatibility ,,

The newly written program can basically give up this writing.

Your code can also be used like this
PHP code
class A {const authKey='1111';}class B extends A{ public function __construct(){  echo parent::authKey;}}new B;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.