首页 > 编程语言 >PHP 类 BaseClass

PHP 类 BaseClass

时间:2023-05-09 23:32:55浏览次数:26  
标签:function __ int Data echo Player PHP BaseClass


1.创建一个简单的类

<?php

class Example
{
	public $item = 'hello zxl';
	public $name;
	function Sample()
	{
		$this->Test();
	}
	function Test()
	{
		echo 'ok' . "<br />";
		echo $this->item;
		$regular = 100;
		echo $regular;
	}

}

$e = new Example();
$e->Sample();
?>



2.类-类型引用

<?php

//Type Hinting
class Test
{
	public function __construct()
	{
	
	}
	public function Write()
	{
		echo 'I am writing form test ' . "<br />";
	}
}

class Foo
{
	public function __construct(Test $a)
	{
		$this->NewObj = $a;
		$this->NewObj->Write();
	}
}

new Foo(new Test);

?>



3.类中的各种内置方法

pt1

<?php
//Magic Methods

class Test
{
	public $name;
	public function Hello()
	{
		echo 'hello';	
	}
	function __get($param)
	{
		echo "$param does not exist";
	}
	function __set($name, $value)
	{
		echo "you were going to set a property of $name - > $value";
		$this->{$name} = $value;
	}
	function __call($param, $value)
	{
		echo "you were going to process $param($value)";
		print_r($value);
	}
}

$test = new Test();
$test->Hello();
$test->nzzzame; //this is not in class goto "__get()"
$test->age=10; //this is not in class goto "__set()"
$test->Something('zzz','xxx','lll'); //this is not in class goto "__call()"


?>



pt2

<?php
//Magic Methods

class Test
{
	
	function __construct()
	{
		echo 'item created' . "<br />";	
	}
	function __destruct()
	{
		echo 'item erased' . "<br />";	
	}
	function __toString()
	{
		return 'this is tostring' . "<br />";	
	}
	function __clone()
	{
		echo '1' . "<br />";
	}
	
}

$test = new Test();
echo $test;
$test2 = clone $test;

?>


4.简单的文件读写类


Logger.php

<?php
//Logger
require_once('Log.class.php');

$log = new Log();
$log->Write('test.txt' , 'zzzzz');

//echo $log->Read('test.txt');


?>



Log.class.php

<?php
//Logger
class Log
{
private $_FileName;
private $_Data;
/**
*@desc writes to a file
*@param str $strFileName the name of the file
*@param str $strData data to be appended to the file
*/

	public function Write($strFileName , $strData) 
	{
		$this->_FileName = $strFileName;
		$this->_Data = $strData;

		$this->_CheckPermission();
		$this->_CheckData();

		$handle = fopen($strFileName, 'a+');
		fwrite($handle, $strData . "\r\n");
		fclose($handle);
	}
	public function Read($strFileName)
	{
		$this->_FileName = $strFileName;
		
		$this->_CheckExists();		

		$handle = fopen($strFileName , 'r');
		return file_get_contents($strFileName);
	}
	private function _CheckExists()
	{
		if(!file_exists($this->_FileName))
		die('the file does not exist ');
		
	}
	private function _CheckPermission()
	{
		//if(!is_writable($this->_FileName))
		//die('Change you chmod permissions to ' . $this->_FileName);
		
	}
	private function _CheckData()
	{
		if(strlen($this->_Data) < 1 )
		die('null ' . $this->_Data);
		
	}
}

?>



5.数组参数的运算类

<?php

//Decorator Pattern
class Player
{
	public $Data = array();
	
	public function __construct(array $info)
	{
		$this->Data = $info;
	}
}

abstract class Player_Decorate
{
	abstract public function Add($int);
}


class Player_Str_Decorate extends Player_Decorate
{
	public function __construct(Player $p)
	{
		$this->Player = $p;
		//$this->Player->Data['str'] +=5 ;
	}
	
	public function Add($int)
	{
		$this->Player->Data['str'] += $int;
	}
	
}

class Player_Dex_Decorate extends Player_Decorate
{
	public function __construct(Player $p)
	{
		$this->Player = $p;
		//$this->Player->Data['dex'] +=5 ;
	}
	
	public function Add($int)
	{
		$this->Player->Data['dex'] += $int;
	}
	
	
}

$P = new Player(array('str' => 10,'dex' => 15));
echo $P->Data['str'];
echo '<br />';
echo $P->Data['dex'];

echo '<hr />';

$Str = new Player_Str_Decorate($P);
$Str->Add(25);
echo $Str->Player->Data['str'];

echo '<br />';

$Dex = new Player_Dex_Decorate($P);
$Dex->Add(25);
echo $Dex->Player->Data['dex'];




?>



6.算数运算类

<?php
//Scop & Calculator
class Calc
{
	public $input;
	public $input2;
	private $output;

	function setInput($int)
	{
		$this->input = (int) $int;
	}
	function setInput2($int)
	{
		$this->input2 = (int) $int;
	}
	function calculate()
	{
		$this->output = $this->input * $this->input2;
	}
	function getResult()
	{
		return $this->output;
	}
	
}

$e = new Calc();
$e->setInput(5);
$e->setInput2(55);
$e->calculate();
echo $e->getResult();
?>



7.php类中链的方法

chain_Methods.php

<?php
//chain methods
require('cupcake.php');
$cupcake = new Cupcake();

$cupcake->Nuts('10')
		->Frosting('chocolate')
		->Sprinkles('200');

print_r($cupcake->Cupcake);

?>




cupcake.php

<?php
//chain methods
class Cupcake
{

	public $Cupcake = array();
	
	public function Frosting($str)
	{
		$this->Cupcake['Frosting'] = $str;
		return $this;
	}
	public function Nuts($int)
	{
		$this->Cupcake['Nuts'] = (int) $int;
		return $this;
	}
	public function Sprinkles($int)
	{
		$this->Cupcake['Sprinkles'] = (int) $int;
		return $this;
	}

}
?>









标签:function,__,int,Data,echo,Player,PHP,BaseClass
From: https://blog.51cto.com/u_5673546/6260070

相关文章

  • php获取未解码之前的原始接口请求参数
    前言目前的几个项目,业务方基本都使用POST方式请求接口,我们本机磁盘会保留一份请求的原始参数用于请求分析和问题排查使用,一般有问题,也会基于seqid(请求唯一id)捞到日志,copy参数模拟请求看是否复现,但一直有个比较蛋疼的问题,PHP的$_POST,$_GET,$_REQUEST这些获取参数的方法获取到的......
  • PHP:cURL error 60: SSL certificate unable to get local issuer certificate](转)
    原文:https://www.cnblogs.com/xiaofeilin/p/14128025.html1、问题导致该问题的原因在于没有配置curl.cainfo,该配置位于php.ini中2、解决下载cacert.pemhttps://curl.haxx.se/ca/cacert.pem配置php.ini[curl];AdefaultvaluefortheCURLOPT_CAINFOoption.Thisis......
  • php获取1688阿里巴巴关键字搜索新品数据API接口、获取上新关键词推荐、获取宝贝详情数
    ​ php的主要优势以及特点: 便于学习和使用:PHP是一门非常容易学习和使用的语言,其语法和结构都非常简单。具有广泛的应用范围:PHP可以用于开发各种类型的Web应用,如博客系统、内容管理系统、电子商务网站、社交网络等。巨大的社区支持:有一个庞大的PHP社区,提供了大量的......
  • php:7-cli-apline安装mysql redis mongo扩展模块
    apkadd--no-cachebuild-dependenciesbuild-baseopenssl-devautoconfg++libtoolmakecurl-devlibxml2-devlinux-headersdocker-php-ext-install-j2mysqlidocker-php-ext-installpdo_mysqlpeclinstallmongodb-1.2.2echo"extension=mongodb.so"......
  • PHP trait使用
    一、trait、继承、实例化三者的区别对于当前一个类需要用到另一个或多个类的方法的情况,我们一般会想到的方式有继承、直接实例化另外一个或多个类等等的方法,来对比一下这些方法和Trait类的区别:继承:对于继承,可以完美地复用另一个类的一些方法,但是对于需要复用多个类的方法时,PHP......
  • [李景山php] 20170504深入理解PHP内核[读书笔记]--第一章准备工作和背景知识--2
    第一节:环境搭建编译安装的关键点:配置编译安装环境,build-essential环境。1.1准备编译环境针对于ubuntu16.04下面建设编译安装环境:apt-getinstallbuild-essential1.2编译cd~/php-src./buildconf./configure–help#查看可用参数./configure–disable-all#编......
  • PHP Windows 下 XAMPP 的 xdebug 配置
    在IntelliJ下调试PHP的断点有时候还是比较困惑的。同时根据你使用的xdebug配置也有关系。xdebug2.x下面的配置是xdebugVersion2的配置,如果你使用xdebug3.x版本的话,配置是不同的。[XDebug]zend_extension="php_xdebug.dll"xdebug.remote_autostart=1xdebug.profil......
  • PHP 高精度计算
    #两个高精度数比较#intbccomp(string$left_operand,string$right_operand[,int$scale])#$left=$right返回0#$left<$right返回-1#$left>$right返回1#$scale小数点位数#两个高精度数相加#stringbcadd(string$left_operand,string$right_o......
  • phpstorm导出导入设置
    导出设置到JAR文档要导出IDE设置到一个JAR文档在主菜单,选择File|ExportSettings在打开的ExportSettings对话框,指定要导出的设置项通过选择它们旁边的复选框。默认的,所有设置项都已选中。在Exportsettingsto文本框,为目标存档指定完全合适的名称。手动的输入路径或点击Browse......
  • workerman下框架gateway报错 worker[thinkphp:30776] exit with status 64000
    wokerman启动之后一直报错Worker[30477]processterminatedworker[thinkphp:30477]exitwithstatus64000Worker[30533]processterminatedworker[thinkphp:30533]exitwithstatus64000Worker[30571]processterminatedworker[thinkphp:30571]exitwithstatus64......