How can I give a large number of class methods "almost" the same code

class myclass{ function name($val){ echo "this is name method and the value is".$val; } function password($val){ echo "this is password method and the value is".$val; } }

and here is how to use it:

$myclass= new myclass(); $myclass->name("aaa")//output: this is name method and the value is aaa

it works fine because I just have 2 methods "name" and "password" what If I have a huge number of methods, it would not be easy to add those methods to my class and write the same code for each method, I want to change my class to let each method give the same output apart from the method name? and I don't want to write all details for all methods because they are almost similar,is this possible in PHP? I wish I was clear :)

--------------Solutions-------------

You could override the __call() method for the class, which is a "magic method" that will be used when a non-existent method is called.

Use the __call magic method, like the following:

class myclass
{
function __call($func, $args)
{
echo 'this is ', $func, 'method and the value is', join(', ', $args);
}
}

This function will get called for any function that doesn't have an explicit function definition.

Note that $args is an array containing all the parameters the function's called with.

Category:php Time:2009-02-10 Views:1
Tags: php oop

Related post

  • How to pass large number of parameters in stored procedure from code to MS SQL 2010-06-07

    How to pass a large number of parameters (say 20+) to stored procedure from code. Like we can group all the parameters in one class object and then pass it across, but how to implement such this in case of stored procedures. Currently I have to creat

  • How well will WCF scale to a large number of client users? 2008-09-04

    Does anyone have any experience with how well web services build with Microsoft's WCF will scale to a large number of users? The level I'm thinking of is in the region of 1000+ client users connecting to a collection of WCF services providing the bus

  • Does SQL Server 2005 scale to a large number of databases? 2008-09-15

    If I add 3-400 databases to a single SQL Server instance will I encounter scaling issues introduced by the large number of databases? --------------Solutions------------- This is one of those questions best answered by: Why are you trying to do this

  • Managing authorized_keys on a large number of hosts 2008-09-16

    What is the easiest way to manage the authorized_keys file for openssh across a large number of hosts? If I need to add or revoke a new key to an account on 10 hosts say, I must login and add the public key manually, or through a clumsy shell script,

  • Best approach for redirecting a large number of old URLs to new URLs? 2008-10-28

    We are re-platforming for a client, and they are concerned about SEO. Their current site supports SEO friendly URLs, and so does the new platform. So for those, we are just going to create the same URL mapping. However, they have a large number of ot

  • Store large number of data points? 2008-11-01

    What is the best way to store a large number of data points? For example temperature values which are measured every minute over lots of locations? SQL databases with one row per data points doesn't seem very efficient. --------------Solutions-------

  • ActiveMQ throws OOME when creating a large number of topics 2008-12-18

    I'm using ActiveMQ 5.2 and my application requires a large number of topics, somewhere in the neighbourhood of 500,000. When I run my application, after creating only about 1000 topics, ActiveMQ throws an OutOfMemoryException. Is it unreasonable to e

  • Selecting a large number of rows given the primary key 2009-01-18

    I need to select a large number of rows from an even larger table which is keyed on an autoincrement identity column. I have the primary key values for each of the rows that I'm trying to select, but it could be very large. Often, but not always, the

  • What's the fastest way to test the validity of a large number of well-formed URLs 2009-02-18

    My project requires me to validate a large number of web URLs. These URLs have been captured by a very unreliable process which I do not control. All of the URLs have already been regexp validated and are known to be well-formed. I also know that the

  • Keep a large number of objects in memory for a long time 2009-02-19

    In my ASP.NET app, I have a dictionary that contains a large number of objects (let's say as large as 1M, could get even bigger later), the object stored in the dictionary is of reference type not struct type, the dictionary is meant to work as a cac

  • ASP.Net - Good UI Design Question for Managing large number of items 2009-02-23

    We're currently working on a solution that involves managing a large number of parts for a project. In our database, we have a project table and we have a parts table. Those parts can be assigned to multiple projects and vise-versa. This is done thro

  • Best approach to separate and re-factor a large number of dependent classes 2009-03-05

    I have a v-large number of hierarchical structures (or DTO's) that are shared across different business logic, application tiers, web service and WCF contracts. I want to refactor all of my code to split the structures into discreet business domain a

  • Best way to keep a large number of hobby projects alive; open sourcing? 2009-03-08

    Because my time is limited I can usually only focus on one or two of my hobby projects, while the others sit there wasting away. I am looking for a solution that would allow me to divide my time better. is open sourcing where I take the role of guidi

  • What is the fastest way to read a large number of small files into memory? 2009-03-09

    I need to read ~50 files on every server start and place each text file's representation into memory. Each text file will have its own string (which is the best type to use for the string holder?). What is the fastest way to read the files into memor

  • What to do when you need to store a (very) large number? 2009-03-27

    I am trying to do a Project Euler problem but it involves adding the digits of a very large number. (100!) Using Java int and long are too small. Thanks for any suggestions --------------Solutions------------- Class BigInteger looks like it might be

  • A Large Number of sp_counted_impl_p Objects 2009-03-31

    I just performed Allocation Profiling about how many objects of each type are in my application. I am using boost::shared_ptr extensively. I found a large number of sp_counted_impl_p objects allocated, each occupying 16 bytes. How many of sp_counted_

  • Database Structures with a Large Number of Bit Fields 2009-04-08

    I have a class of data with a very large number of binary properties--151 (!) to be exact--and I am concerned with how to model this data structurally. Despite the internal efficiencies of storing bit-fields as bytes, my programming spidey senses are

  • What is the best method in passing a large number of parameters in SQL Server 2009-05-08

    I'm trying to work out what the best method to passing a large number of parameters into a stored procedure. Some methods i was thinking about are; Create the relevant database objects, including a parameter object for each item and invoke the comman

  • Modulo in JavaScript - large number 2009-05-30

    I try to calculate with JS' modulo function, but don't get the right result (which should be 1). Here is a hardcoded piece of code. var checkSum = 210501700012345678131468; alert(checkSum % 97); Result: 66 Whats the problem here? Regards, Benedikt --

Copyright (C) pcaskme.com, All Rights Reserved.

processed in 0.537 (s). 13 q(s)