Register Register Member Login Member Login Member Login Forgot Password ??
PHP , ASP , ASP.NET, VB.NET, C#, Java , jQuery , Android , iOS , Windows Phone
 

Registered : 109,027

HOME > PHP Manual > Examples - Examples





ลองใช้ค้นหาข้อมูล


   
Bookmark.

Examples

Table of Contents

Object Aggregation examples

An Association is a composition of independently constructed and externally visible parts. When we associate classes or objects, each one keeps a reference to the ones it is associated with. When we associate classes statically, one class will contain a reference to an instance of the other class. For example:

Example #1 Class association

<?php
class MyDateTime {
  
  function 
MyDateTime() 
  {
      
// empty constructor
  
}

  function 
now() 
  {
      return 
date("Y-m-d H:i:s");
  }
}

class 
Report {
  var 
$_dt;
  
// more properties ...

  
function Report() 
  {
      
$this->_dt = new MyDateTime();
      
// initialization code ...
  
}

  function 
generateReport() 
  {
      
$dateTime $this->_dt->now();
      
// more code ...
  
}

  
// more methods ...
}

$rep = new Report();
?>
We can also associate instances at runtime by passing a reference in a constructor (or any other method), which allow us to dynamically change the association relationship between objects. We will modify the example above to illustrate this point:

Example #2 Object association

<?php
class MyDateTime {
  
// same as previous example
}

class 
MyDateTimePlus {
  var 
$_format;
  
  function 
MyDateTimePlus($format="Y-m-d H:i:s"
  {
      
$this->_format $format;
  }

  function 
now() 
  {
      return 
date($this->_format);
  }
}

class 
Report {
  var 
$_dt;    // we'll keep the reference to MyDateTime here
  // more properties ...

  
function Report() 
  {
      
// do some initialization
  
}

  function 
setMyDateTime(&$dt
  {
      
$this->_dt =& $dt;
  }

  function 
generateReport() 
  {
      
$dateTime $this->_dt->now();
      
// more code ...
  
}

  
// more methods ...
}

$rep = new Report();
$dt = new MyDateTime();
$dtp = new MyDateTimePlus("l, F j, Y (h:i:s a, T)");

// generate report with simple date for web display
$rep->setMyDateTime(&$dt);
echo 
$rep->generateReport();

// later on in the code ...

// generate report with fancy date
$rep->setMyDateTime(&$dtp);
$output $rep->generateReport();
// save $output in database
// ... etc ... 
?>

Aggregation, on the other hand, implies encapsulation (hidding) of the parts of the composition. We can aggregate classes by using a (static) inner class (PHP does not yet support inner classes), in this case the aggregated class definition is not accessible, except through the class that contains it. The aggregation of instances (object aggregation) involves the dynamic creation of subobjects inside an object, in the process, expanding the properties and methods of that object.

Object aggregation is a natural way of representing a whole-part relationship, (for example, molecules are aggregates of atoms), or can be used to obtain an effect equivalent to multiple inheritance, without having to permanently bind a subclass to two or more parent classes and their interfaces. In fact object aggregation can be more flexible, in which we can select what methods or properties to "inherit" in the aggregated object.



ThaiCreate.Com Forum


Comunity Forum Free Web Script
Jobs Freelance Free Uploads
Free Web Hosting Free Tools

สอน PHP ผ่าน Youtube ฟรี
สอน Android การเขียนโปรแกรม Android
สอน Windows Phone การเขียนโปรแกรม Windows Phone 7 และ 8
สอน iOS การเขียนโปรแกรม iPhone, iPad
สอน Java การเขียนโปรแกรม ภาษา Java
สอน Java GUI การเขียนโปรแกรม ภาษา Java GUI
สอน JSP การเขียนโปรแกรม ภาษา Java
สอน jQuery การเขียนโปรแกรม ภาษา jQuery
สอน .Net การเขียนโปรแกรม ภาษา .Net
Free Tutorial
สอน Google Maps Api
สอน Windows Service
สอน Entity Framework
สอน Android
สอน Java เขียน Java
Java GUI Swing
สอน JSP (Web App)
iOS (iPhone,iPad)
Windows Phone
Windows Azure
Windows Store
Laravel Framework
Yii PHP Framework
สอน jQuery
สอน jQuery กับ Ajax
สอน PHP OOP (Vdo)
Ajax Tutorials
SQL Tutorials
สอน SQL (Part 2)
JavaScript Tutorial
Javascript Tips
VBScript Tutorial
VBScript Validation
Microsoft Access
MySQL Tutorials
-- Stored Procedure
MariaDB Database
SQL Server Tutorial
SQL Server 2005
SQL Server 2008
SQL Server 2012
-- Stored Procedure
Oracle Database
-- Stored Procedure
SVN (Subversion)
แนวทางการทำ SEO
ปรับแต่งเว็บให้โหลดเร็ว


Hit Link
   







Load balance : Server 03
ThaiCreate.Com Logo
© www.ThaiCreate.Com. 2003-2024 All Rights Reserved.
ไทยครีเอทบริการ จัดทำดูแลแก้ไข Web Application ทุกรูปแบบ (PHP, .Net Application, VB.Net, C#)
[Conditions Privacy Statement] ติดต่อโฆษณา 081-987-6107 อัตราราคา คลิกที่นี่