Subclasses use parent class variables to solve ideas

Source: Internet
Author: User
Subclasses use parent class variables
Class A {
var $authKey = ' 1111 ';

}

Class B extends a{
__construct () {
Echo Parent::authkey;
}
}
Error undefined class constant ' AuthKey '

------Solution--------------------
The error message means a constant that is undefined. You're missing a $.
But $authkey is not a static variable, so you can't call it so statically
The right approach
PHP code
class A {//Do not recommend using VAR in class to declare the variable public $authKey = ' 1111 ';}   Class B extends a{//b will have all non-private members of a public function __construct () {echo $this->authkey; }} 
------solution--------------------
Var was in the previous version of PhP4 and was later omitted.
------Solution--------------------
var is best taken with the case! Some of the lower versions support
------Solutions--------------------
var is php 4.X,, 5+ used in order to backward compatibility,

New program, basically can give up this type of writing

Your code can also use
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.