PHP based object-oriented programming (a) with the object class

The process of creating an object is instantiated class.
The class must be instantiated to access the inside of the properties and methods.

Attributes

Variable members of a class called "Properties", or "field", "features", in this document referred to collectively as "property." Statement by the protected property or keywords public,
beginning private, then with a normal variable declaration to compose. Attributes can initialize a variable, but the value initialization must be a constant, constant here is the PHP
script when compiling the stage can get its value, without depending on the information to be evaluated at runtime.

Attribute declaration must be based on access control at the beginning of the
public public property, external access to
private private property, allow access only to the class calls
protected protected, allowing only this class or sub-class access
attribute support scalar (integer, floating point, character string, Boolean), compound types: arrays and objects

method

The method is a class in the "function" only through the object calls

The method also must begin with control characters visit: public, private, protected

Access properties and methods

Object-access operator ->to directly access property, or to perform a method
in which members of the class of the method can be used ->(对象运算符):$this->property(其中 property 是该属性名)in this way to access the non-static properties. $thisIs a pseudo-variable, always refers to the current object.
In the external access class property or method, the object can directly access the property or method within the class, you must use $this
the static property is ::(双冒号):self::$propertyaccessed.
The object is a reference variable, we assign to objects and not create a new variable, but to create a reference to the current object

6 ways to create objects

<?php
{
	public $name ='My name is Tom'
	public function getName()
	{
		return $this->name;
	}
	public function getObj()
	{
		return new self();
	}
	public function getStatic()
	{ 
		return new static();
}

class Demo2 extends Demo1 //extends继承上个类
{
	public function getNewObj()
	{
	return new parent();
	}
}

1, with the new class name () to create an object

$obj = new Demo1();
echo $obj->name;

2, a class name, a string in a manner variable

$className = 'Demo1';
$obj1= new $className();
echo $obj1 ->name;

3, with the object to create an object, and it creates a new object

$obj2 = new $obj();
echo $obj2->name;
echo get_class($obj2);

4、new self()

$obj3 = $obj->getObj;
echo $obj3->name;
echo get_class($obj2);

5, with a new parent () to create objects

$obj4=(new Demo2)->getNewObj()
echo $obj4->name;

6, to create a new static class based on the current call ()

$obj5 = (new Demo1)->getStatic;
$obj6 = (new Demo1)->getObj();
// new static 创建的对象,直接与调用者绑定,静态延迟绑定
$obj7 = (new Demo2)->getStatic;
$obj8 = (new Demo2)->getObj();

Class constants

In the class is always the same constant value, use const created, without the $ symbol, must be initialized

Access Method:

Internal Access

self :: Class constant name

External Access

Class constant name like name ::
class variables constant name :: class
Object :: constants class name

Class constructor and destructor method

Constructor: it is used to instantiate object creation of

__construct()

Destructor: to destroy the object

__destruct()

class Staff //声明一个员工类
{
	public $name;
	public $age;
	public $salary;
	//构造方法使用的固定方法名:__construct()
	public function __construct($name,$age,$salary)
	{	
	//构造方法:通常是用来初始化对象中的属性
	$this->name =$name;
	$this->age =$age;
	$this->salary=$salary;
	}
	//析构方法:对象销毁时自动调用,没有参数,__destruct()
	public function __destruct()
	{
		echo '对象被销毁'
	}
}
//创建一个对象,来访问类中的属性
$obj = new Staff('Peter',28,3500

The method of encapsulation and object magic

class Staff //声明一个员工类
{
  private $name;
  private $age;
  private $salary;

  //编写方法来访问对象的私有属性
  public function getName()
  {
  	return $this->name;
  }
  //使用魔术方法__get来直接从外部访问对象的私有属性
  public function __get($a)
  {
  	return $this->a;	
  }

  //编写方法来设置对象的私有属性赋值
  public function setName($name,$value)
  {
  $this->$name =$value;
  }
  //使用魔术方法__set来直接给私有属性赋值
  public function __set($a,$value)
  {
  $this->$a =$value;
  }
}

$obj = new Staff('Peter',28,3500echo $obj->getName();//使用编写的方法访问
echo $obj->age;//使用魔术方法直接访问对象的属性
echo $obj->salary;//使用魔术方法直接访问对象的属性
echo $obj->setName('name',Tom);//使用编写的方法设置对象属性
echo $obj->age = 45;//使用魔术方法直接设置不能访问的属性
echo $obj->salary= 8000;//使用魔术方法直接设置不能访问的属性


Magic like this method as well as __construct (), __destruct (), __call (), __callStatic (), __ get (), __set (), __isset (), __unset (), __sleep (), __wakeup (), __toString (), __invoke (), __set_state (), __clone () and __debugInfo () method and the like.

Class inheritance

extends keyword

 class 子类 extends 父类

Creating a subclass is to expand the functionality of the parent class to achieve code reuse
(today a little bit lazy, copy and paste it rookie tutorial)

<?php
class BaseClass {
   function __construct() {
       print "BaseClass 类中构造方法" . PHP_EOL;
   }
}
class SubClass extends BaseClass {
   function __construct() {
       parent::__construct();  // 子类构造方法不能自动调用父类的构造方法
       print "SubClass 类中构造方法" . PHP_EOL;
   }
}
class OtherSubClass extends BaseClass {
    // 继承 BaseClass 的构造方法
}

// 调用 BaseClass 构造方法
$obj = new BaseClass();

// 调用 BaseClass、SubClass 构造方法
$obj = new SubClass();

// 调用 BaseClass 构造方法
$obj = new OtherSubClass();
?>

PHP will not call the parent class constructor automatically in the constructor of the subclass. To execute the parent class constructor, you need to call the constructor of the subclassparent::__construct()

Static (Static) Keyword

Declaring class property or method static, you can not instantiate the class and direct access. Static properties of a class can not be instantiated object to access (but static methods)
due to the static method does not need to call through the object, the pseudo-variable $ this is not available in a static method. Accessing> operator - not the object through the static property.

Access control characters: Indicates where class members can be accessed: public / private / protected
members of the status symbol: Indicates how to access the member: static self / parent / static non-static: $ this->

The difference between self, parent and static of

self :: access static properties of this class
parent :: parent class be statically bound
static :: static properties of the current class binding, and the former two different static corresponding class is dynamically set, determined by the calling class , if the self and parent are statically bound to that kind of thing, static to dynamic binding class is called static delay binding (late static binding)
(self and parent) bind them and the class code is statically bound compilation stage, while the static binding class is bound at runtime code, so called: static delay (different timing and type of binding) binding

Namespaces

PHP namespaces can solve the following two problems:

Name conflict between code written in PHP and internal users 1. class / functions / constants or third-party classes / functions / constants.
2. long identifier names (usually to alleviate the problem of the definition of first class) to create a name alias (or short) to improve the readability of the source code.

<?php  
namespace MyProject;

const CONNECT_OK = 1;
class Connection { /* ... */ }
function connect() { /* ... */  }

namespace AnotherProject;

const CONNECT_OK = 1;
class Connection { /* ... */ }
function connect() { /* ... */  }
?>  

Object traversal

class Demo
{
	public $name;
	public $age;
	public $salary;
	private $sex;//私有属性
	protected $isMarried;//受保护属性
	public static $home;//静态属性
//声明构造方法,用来初始化属性
	public function __construct($name,$age,$salary)
	{	
	$this->name =$name;
	$this->age =$age;
	$this->salary=$salary;
	$this->sex =$sex;
	$this->isMarried =$isMarried;
	self::$home =$home;
	}
	//声明一个query方法,用来在类的内部遍历属性
	public function query()
	{
		print '遍历出对象中的全部属性,包括私有和受保护的:<br>';
		foreach ($this as $key=>$value ){
			print $key.'=>'.$value.'<br>';
	}
	print self::$home;
	}
}
//外部访问
$obj = new Demo('peter',28,3800,'male',true,'shenyang');
//遍历对象
echo '外部访问公共属性:<br>';
foreach ($obj as $key => $value){
	echo $key.'=>'.$value.'<br>';
}
echo Demo::$home;//外部使用类名访问静态成员
echo '<hr>';
$obj->query();//遍历出对象中的全部属性

Released seven original articles · won praise 3 · Views 210

Guess you like

Origin blog.csdn.net/k851819815/article/details/104546853