(new isyeri(new isci( $isciID )))->mesaiEkle([tarih'=> '201-08-15', 'saat'=>2]);
bu tür kullanımlar bana kontrolsüz geliyor, dikkat etmenizde fayda var..
classı biraz değiştirdim, bu tür kullanımda yapabilirsiniz..
/*
Utility class #1
*/
class hammer
{
public function bang(){
echo 'Bang! ';
return;
}
public function pry(){
echo 'Pry! ';
return;
}
}
/*
Utility class #2
*/
class screwdriver
{
public function loosen(){
echo 'Loosen! ';
return;
}
public function tighten(){
echo 'Tighten! ';
return;
}
}
class handyman
{
public function __construct()
{
foreach(func_get_args() as $v)
{
is_object($v) && $this->{get_class($v)} = $v;
}
return $this;
}
public function __set($k,$v) {
if (!property_exists($this,$k)) {
$this->{$k} = $v;
}
}
public function __get($k) {
if (property_exists($this,$k)) {
return $k;
}
throw new Exception(null);
}
}
$me = new handyman(new hammer(), new screwdriver(),....);
$me->screwdriver->loosen();
$me->hammer->bang();
$me->baska; // hata
tüm yorum yapan arkadaşlara teşekkürler. buradaki amacın iç içe kullanmayı başarabilmekti.
Yukarıda sehzadem 'in örneğindeki gibi bir kullanımda da sınıflar ve fonksiyonlar normal bir şekilde çalışmaktadır.
saintx 'in gönderdeği kod bloğu ve diğer örnek ile istediğimi yapmayı başardım. ihtiyacı olan arayan varsa yardımcı olması dileğiyle.
herkese çok teşekkürler.
saintx :
<?php
function with($class)
{
return $class;
}
class Company
{
private $_employees;
public function __construct($employee = null)
{
if ($employee instanceof Employee)
{
$this->setEmployee($employee);
}
}
public function getEmployee($id)
{
return $this->_employees[$id];
}
public function getEmployees()
{
return $this->_employees;
}
public function setEmployee(Employee $employee)
{
if ( ! is_array($this->_employees))
{
$this->_employees = [];
}
$this->_employees[$employee->getId()] = $employee;
return $employee;
}
}
class Employee
{
private $_id;
private $_name;
private $_surname;
public function __construct($id)
{
return $this->setId($id);
}
public function getId()
{
return $this->_id;
}
public function setId($id)
{
$this->_id = $id;
return $this;
}
public function getName()
{
return $this->_name;
}
public function setName($name)
{
$this->_name = $name;
return $this;
}
public function getSurname()
{
return $this->_surname;
}
public function setSurname($surname)
{
$this->_surname = $surname;
return $this;
}
}
$company = new Company();
$company->setEmployee(new Employee(1))->setName('Metin');
$company->setEmployee(new Employee(2))->setName('Ali');
$company->setEmployee(new Employee(3))->setName('Feyyaz');
print_r($company->getEmployee(2)->getName()); // Ali
http://www.ozzu.com/programming-forum/php-create-new-class-instance-inside-another-class-t99319.html
<?php
/*
Utility class #1
*/
class hammer
{
public function bang(){
echo 'Bang! ';
return;
}
public function pry(){
echo 'Pry! ';
return;
}
}
/*
Utility class #2
*/
class screwdriver
{
public function loosen(){
echo 'Loosen! ';
return;
}
public function tighten(){
echo 'Tighten! ';
return;
}
}
/*
Implementer of utility classes
*/
class handyman
{
/*
A place to keep our utilities
*/
public $toolbelt = array();
public function __construct($tools = array())
{
foreach($tools as $tool)
{
/*
http://www.php.net/get_class
Make sure we only have one of any given tool, does not _have_ to be this way
*/
$this->toolbelt[get_class($tool)] = $tool;
}
return $this;
}
/*
http://www.php.net/manual/en/language.oop5.overloading.php#language.oop5.overloading.methods
*/
public function __call($method_name, $args)
{
foreach($this->toolbelt as $tool)
{
/*
http://www.php.net/is_callable
See if the utility object has a callable method matching the one asked for
*/
if(is_callable(array($tool, $method_name)))
{
/*
http://www.php.net/call_user_func_array
Forward the method call to the utility object along with the arguments
*/
return call_user_func_array(array($tool, $method_name), $args);
}
}
/*
http://www.php.net/trigger_error
*/
trigger_error("Do not have a tool that can $method_name!", E_USER_ERROR);
}
}
$me = new handyman(array(new hammer(), new screwdriver()));
$me->bang();
$me->pry();
$me->tighten();
$me->loosen();
$me->gitrdone();
?>