473,396 Members | 1,892 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

PHP fluent interface check if method exists

150 100+
i need to check if there is a method exists on my class or not before i call it here is my code :

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. class elements{
  3.     function add($t){
  4.         if(class_exists($t)){
  5.             $c = new $t;
  6.             return $c;
  7.         }else{
  8.             echo 'class dos\'not exists';
  9.         }
  10.     }
  11. }
  12. class qw{
  13.     function render(){
  14.         echo 'ahmed saber amin';
  15.     }
  16. }
  17. $e = new elements();
  18. $e->add('qw')->renders();
  19. ?>
  20.  
Aug 3 '09 #1
12 4279
Dormilich
8,658 Expert Mod 8TB
to check for a method either use method_exists() or check against an interface.
Aug 3 '09 #2
smartic
150 100+
in my case where i would check in my code ?
Aug 4 '09 #3
Dormilich
8,658 Expert Mod 8TB
at latest just before you call the method, but it depends on the real code and what you want to do there.
Aug 4 '09 #4
Markus
6,050 Expert 4TB
Also - go PHP5 :)
Aug 4 '09 #5
smartic
150 100+
anopther question is there away to declare dynamic property ex :

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. class a{
  3.     function add($pro){
  4.         $this->$pro= 'ex@ex.com';
  5.     }
  6. }
  7. class e extends a{
  8.     function __constract(){
  9.         $this->add('email');
  10.     }
  11. }
  12.  
  13. $e = new e();
  14.  
  15. echo $e->email;
  16.  
  17. ?>
Aug 4 '09 #6
Dormilich
8,658 Expert Mod 8TB
not as a real property, but you can use __get() to work around that.
Aug 4 '09 #7
Markus
6,050 Expert 4TB
@Dormilich
Not if he's using php 4, which I suspect he is. I know, in PHP 5, you can do $this->x = $x, even if you have not explicitly declared the $x member - not sure if this works in PHP 4, though.. I doubt it.
Aug 4 '09 #8
Dormilich
8,658 Expert Mod 8TB
@Markus
then line #8 wouldn’t make sense… but yes, PHP4’s object support is rather, er, low.
Aug 4 '09 #9
smartic
150 100+
my problem is i need parent class to hold dynamic property with out declaring it by var keyword,and also get the last property was added to the parent class here is my code :

Expand|Select|Wrap|Line Numbers
  1. class parts{
  2.     // set part type & name
  3.     public function set($type,$name){
  4.         if(class_exists($type)){
  5.             $this->{$name}['type'] = $type;
  6.             return new $type();
  7.         }
  8.     }
  9. }
  10. class playstation extends parts{
  11.     function speed(){
  12.         // here is my problem & get last var
  13.         print_r($this->emulator);
  14.     }
  15. }
  16. $e = new parts();
  17. // testing
  18. $e->
  19.     set('playstation','emulator')->
  20.     speed('1028');
  21.  
Aug 5 '09 #10
Dormilich
8,658 Expert Mod 8TB
@smartic
working with unknown properties is a bit difficult, why don't you use an array for that?

Expand|Select|Wrap|Line Numbers
  1. class whatever
  2. {
  3.     protected $props = array();
  4.  
  5.     public function __set($name, $value)
  6.     {
  7.         $this->props[$name] = $value;
  8.     }
  9.  
  10.     public function getLast($name)
  11.     {
  12.         return end($this->props);
  13.     }
  14. }
Aug 5 '09 #11
smartic
150 100+
because i want to access it from outside the class like that :
$e->emulator['type']; not $e->$props['emulator']['type'];
Aug 5 '09 #12
Dormilich
8,658 Expert Mod 8TB
@smartic
that won't work, because the property is not public. let's extend the previous code a little bit.
Expand|Select|Wrap|Line Numbers
  1. class whatever
  2. {
  3.     # the array where we store the properties
  4.     protected $props = array();
  5.  
  6.     # the definer function
  7.     public function __set($name, $value)
  8.     {
  9.         $this->props[$name] = $value;
  10.     }
  11.  
  12.     # the accessor function
  13.     public function __get($name)
  14.     {
  15.         if (isset($this->props[$name]))
  16.         {
  17.             return $this->props[$name];
  18.         }
  19.         throw new Exception(1, "The Property '$name' does not exist.");
  20.     }
  21.  
  22.     public function getLast($name)
  23.     {
  24.         return end($this->props);
  25.     }
  26. }
Expand|Select|Wrap|Line Numbers
  1. $e = new whatever;
  2. $e->speed = 1028;
  3. echo $e->speed;
Aug 5 '09 #13

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Farek | last post by:
(If Im posting in the wrong place concerning COM+ and .NET plz redirect me.) Hello all, Im writing a COM+ in VB.NET that is suppose to be able to set/get an address(String value). I've made...
30
by: Rhino | last post by:
I am giving some thought to applying for some jobs that want people with Java and C++ experience. I have been writing Java for several years and am fluent enough that I don't have to get help with...
2
by: threeflush | last post by:
I have some code that checks to see if a document object exists in a sibling frame before continuing processing like: if (parent.frames.document){ .... } However, sometimes the frame I'm...
7
by: yufufi | last post by:
lets say we have a 'shape' class which doesn't implement IComparable interface.. compiler doesn't give you error for the lines below.. shape b= new shape(); IComparable h; h=(IComparable)b;...
20
by: Ole Hanson | last post by:
I am accessing my database through an interface, to allow future substitution of the physical datastore - hence I would like to declare in my Interface that my DAL-objects implementing the...
21
by: Helge Jensen | last post by:
I've got some data that has Set structure, that is membership, insert and delete is fast (O(1), hashing). I can't find a System.Collections interface that matches the operations naturally offered...
11
by: sotto | last post by:
If i have this Interface: Public Interface MyInterface Function test() As Boolean Function test(ByVal MyVar As String) As Boolean End Interface And then i make a Public Class MyOwnClass
4
by: Sohum | last post by:
Hi I'm reasonably new to this group, and to programming in general. I'd like to know, can you selectively scope within an interface? For example: Public Interface MyInterface Friend...
0
by: =?Utf-8?B?amVmZiBt?= | last post by:
Hi there - I'm trying to figure out how to model a fluent interface for asp.net page navigation and am getting stuck - wondering if anyone has ideas on how to accomplish something like the...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.