- PHP 5 CMS Framework Development / 2nd Edition
- Martin Brampton
- 530字
- 2021-04-13 17:22:00
The power and flexibility of an interpreted language is illustrated in PHP by the possibility of indirect references and other similar constructions. An indirect reference is illustrated with the following code:
$field = 'illustration'; $$field = 'a test string'; echo $illustration;
What will happen is that the phrase 'a test string' will be output. This example is artificial, but we will see how powerful this kind of thing is in the next section, when we consider how data objects can be loaded from the database. In fact, with our OO orientation, it will only rarely be necessary to use the double (or more) dollar signs for flexible references. In many contexts, such as referring to an object property or creating a new object, PHP will let us write a variable name where we might ordinarily write an actual name. So, we can refer to $object->$field
when we have previously loaded $field
with the name of one of the properties of $object
. The same technique can be used with method names.
The value of this is clear if we want to create a data object whose properties are determined by the metadata stored in the database. A query will tell us what fields exist for a particular table, and we can use that information to easily create a data object that corresponds to a particular table row. Or, we can create a whole set of such objects. The metadata tells us about any fields that have not been provided in the data submitted to database handling methods, and they can be given default values depending on their type.
PHP will also help us to handle objects whose details we do not know. The function get_object_vars
will return an associative array, where the keys and the names of properties and the values are the current values of the properties. This gives us a way to process all the public properties of an object, without prior knowledge of what they are.
Although not used in this chapter, a similar technique avoids tedious case statements, by using the value of a variable as the name of a method. Suppose we know that an application works differently according to what is in $task
, which may have been obtained from the query string part of the URI. The variable can be used directly to invoke one of a number of possible methods within the current class simply by writing $this->$task()
, possibly with the addition of some parameters. If $task
did not contain the name of a method, the code would fail, but that can be avoided by testing with a standard PHP function method_exists($this, $task)
and raising an error or ignoring the request if the result is not true.
The need to test for valid data is a reminder that there are security considerations for these techniques. If the field that is being used in place of writing the actual name has been received as user input, then it is clearly vital to validate it for acceptable values. In the case of calling a method, a simple test eliminates invalid names. Other security constraints may be needed, according to the circumstances.
- Learning SQL Server 2008 Reporting Services
- Photoshop CC 2017實(shí)戰(zhàn)基礎(chǔ)培訓(xùn)教程(全視頻微課版)
- Photoshop圖形圖像設(shè)計(jì)案例教程(高等院校計(jì)算機(jī)任務(wù)驅(qū)動(dòng)教改教材)
- Animate 2022動(dòng)畫(huà)制作:團(tuán)體操隊(duì)形
- After Effects CC 2019 影視后期特效合成案例教程
- SOLIDWORKS Visualize 實(shí)例詳解(微視頻版)
- 剪映短視頻剪輯與運(yùn)營(yíng)全攻略:視頻剪輯+音頻處理+后期特效+運(yùn)營(yíng)管理
- OpenCV項(xiàng)目開(kāi)發(fā)實(shí)戰(zhàn)(原書(shū)第2版)
- CINEMA 4D R20完全實(shí)戰(zhàn)技術(shù)手冊(cè)
- Python氣象應(yīng)用編程
- MooTools 1.2 Beginner's Guide
- CorelDRAW 2018平面設(shè)計(jì)基礎(chǔ)教程(第3版)
- Illustrator 2022從入門到精通
- AutoCAD 2010 建筑設(shè)計(jì)與制作技能基礎(chǔ)教程
- SQL編程基礎(chǔ)(原書(shū)第3版)