i need to check if there is a method exists on my class or not before i call it here is my code : - <?php
-
class elements{
-
function add($t){
-
if(class_exists($t)){
-
$c = new $t;
-
return $c;
-
}else{
-
echo 'class dos\'not exists';
-
}
-
}
-
}
-
class qw{
-
function render(){
-
echo 'ahmed saber amin';
-
}
-
}
-
$e = new elements();
-
$e->add('qw')->renders();
-
?>
-
12 4279
to check for a method either use method_exists() or check against an interface.
in my case where i would check in my code ?
at latest just before you call the method, but it depends on the real code and what you want to do there.
anopther question is there away to declare dynamic property ex : - <?php
-
class a{
-
function add($pro){
-
$this->$pro= 'ex@ex.com';
-
}
-
}
-
class e extends a{
-
function __constract(){
-
$this->add('email');
-
}
-
}
-
-
$e = new e();
-
-
echo $e->email;
-
-
?>
not as a real property, but you can use __get() to work around that.
@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.
@Markus
then line #8 wouldn’t make sense… but yes, PHP4’s object support is rather, er, low.
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 : -
class parts{
-
// set part type & name
-
public function set($type,$name){
-
if(class_exists($type)){
-
$this->{$name}['type'] = $type;
-
return new $type();
-
}
-
}
-
}
-
class playstation extends parts{
-
function speed(){
-
// here is my problem & get last var
-
print_r($this->emulator);
-
}
-
}
-
$e = new parts();
-
// testing
-
$e->
-
set('playstation','emulator')->
-
speed('1028');
-
@smartic
working with unknown properties is a bit difficult, why don't you use an array for that? - class whatever
-
{
-
protected $props = array();
-
-
public function __set($name, $value)
-
{
-
$this->props[$name] = $value;
-
}
-
-
public function getLast($name)
-
{
-
return end($this->props);
-
}
-
}
because i want to access it from outside the class like that :
$e->emulator['type']; not $e->$props['emulator']['type'];
@smartic
that won't work, because the property is not public. let's extend the previous code a little bit. - class whatever
-
{
-
# the array where we store the properties
-
protected $props = array();
-
-
# the definer function
-
public function __set($name, $value)
-
{
-
$this->props[$name] = $value;
-
}
-
-
# the accessor function
-
public function __get($name)
-
{
-
if (isset($this->props[$name]))
-
{
-
return $this->props[$name];
-
}
-
throw new Exception(1, "The Property '$name' does not exist.");
-
}
-
-
public function getLast($name)
-
{
-
return end($this->props);
-
}
-
}
- $e = new whatever;
-
$e->speed = 1028;
-
echo $e->speed;
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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;...
|
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...
|
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...
|
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
|
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...
|
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...
|
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...
|
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
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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,...
|
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...
|
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...
|
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...
| |