PHP Object-oriented inheritance

Source: Internet
Author: User


Class: Abstracted by many objects.
Object: instantiated through the class
To define a class:
Class Info
{
Const P = 3.1425926; Defines what is commonly used in certain classes as constants for ease of use

public static $class; The static keyword defines the statically
Public $code;
protected $name;
Private $sex;

The static function Test ()//static method
{
echo self:: $class; Call a static member inside a class
echo "static Method! ";
}

function __construct ($s)
{
$this->sex = $s;
}

function Say ()
{
echo self:: $class; Static members can be called in normal methods
echo "Hello";
}

function Getsex ()
{
return $this->sex;
}
function Setsex ($s)
{
$this->sex = $s;
}

function __set ($n, $v)
{
$this $n = $v;
}

function __get ($n)
{
return $this $n;
}
}

Instantiation of objects
$r = new Info ("female");
$r->code = "Join";
$r->sex;
/*info:: $class = "0305";
$r->say (); */

/*info:: $class = "0305"; accessing static members
Info::test (); */

Three major features
Packaging:
Purpose: To make the class more secure, avoid direct access to member variables by the outside world
Procedure: 1. Change member variables to private
2. Indirect manipulation of member variables through methods (functions)

Inherited
Concept: If a class has subclasses, that subclass inherits everything from the parent class (the private member cannot access it).
You need to add a keyword when defining a subclass: Extends
Features: Single inheritance, a son can only have a father, a class can have only one parent class
If there is a constructor in the parent class, the subclass is instantiated according to the criteria of the parent class constructor.
Subclasses override of Parent class methods: Write the same method in a subclass
Final: Used to decorate a class, which means that the class is a final class that cannot be inherited

/*class Test extends Info
{
Public $birthday;

function Run ()
{
echo "Run";
}

function Say ()
{
Parent::say (); Call the Say () method of the parent class
echo "Test Say";
}

}

$t = new Test ("female");
$t->code = "P001";
Echo $t->sex;
$t->say ();
$t->run (); */


Static members
The ordinary members of a class belong to the object, not to the class (called when the object is called)
What is static: A class static member belongs to a class, not to each object.
Defining static members with the Static keyword decoration
Static methods cannot invoke ordinary members
A static method can call a static member and use the Self keyword to invoke
Self represents the class, $this represents the object

PHP Object-oriented inheritance

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.