|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
||||
|
||||
|
Calling a user made functionfunction
I have alot of db queries and alot of them happen regulary. So I decided to create a class with those functions. Since I never really created my functions or class I decided to go with one of the easiest of functions just to test out if i am doing it right. As it turns out I am doing it wrong. The page is giving me
Quote:
Here is the code for botht he class file and the page tryng to czll the function PHP Code:
That was the page callign the function and here is the other file PHP Code:
__________________
CHornJr "One day I'll know what I am doing" ![]() My Blog Suanhacky Lodge #49 Rebel Squadrons |
|
#2
|
|||
|
|||
|
Functions contained within a class are actually called methods. In order to access these methods you have to create a new "instance" of the class and store it in a variable. Then to access a method you use the "->" operator. In your case the code would look something like :
PHP Code:
I hope that helps out! |
|
#3
|
|||
|
|||
|
hmm, to use functions you dont need to use classes!
just use: PHP Code:
![]() |
|
#4
|
||||
|
||||
|
I now got that working but I have another question. In my file I have a number of functions now. Can I get one function to call another function? If I can, can I call function 2 into function 1. For instance:
PHP Code:
|
|
#5
|
|||
|
|||
|
yes, that code would work!
|
|
#6
|
||||
|
||||
|
in the first function I originally had it include a seperate file and it worked fine but i added the contents of that file to my function file as it's own function but it is not working and i am not sure why it isn't.
Here are the two functions PHP Code:
Any ideas as to what I am doing wrong? |
|
#7
|
|||
|
|||
|
what error messages are you getting, and which line??
|
|
#8
|
|||
|
|||
|
On a hunch... try calling your second function like so:
$this->newsType(); See if that works...
__________________
____________________________________________ Developer Shed Weekly Writer | DevArticles Forum Moderator Build Your Own KlipFolio Klip With PHP FrankManno.com - Under Construction Design Interactive Group - Under Construction |
|
#9
|
|||
|
|||
|
After a second look i know what the problem is now!
firstly frankie, $this->newsType(); is used in a class. You would use this when your making a refrence to a function, which is called inside another class funcion. CHornJr, there is a few problems with your code. Firstly a function cant use variables that are not declaired inside the same function. which means PHP Code:
this would result in an error, cos the var $var has not been defined in the function, there are a couple of work arounds here, make the $var a global with global $var; inside the function, before the $var is called, or pass the $var as a argument function Test ($var) {... and when you call the function Test($var); so in your case you would need to declair the $news as a global your function newstype() actually returns a value, meaning, if you where to echo the function, you would echo a value. you need to assign the function to a $variable $var = newstype(); |
|
#10
|
||||
|
||||
|
Ok I am little confused here cause I thought I tried what you said but apparently I didn't do it correctly.
In function viewnewshome I have after I declare $news PHP Code:
|
|
#11
|
|||
|
|||
|
you will need to declair the global in the newstype function
function NewsType() { global $news; .... then the next change is where you have newstype(); in the first function. Calling this function without assigning it to a var, or string doesnt actually do anything, because the variable $newstype has been declaired inside that function, however you return it, so if you change the line newstype(); $newstype = newstype(); it will cause your function to work. if you make those 2 changes your code should then work |
|
#12
|
||||
|
||||
|
Ok it is jumping straight to the else statmement in the second function
|
|
#13
|
|||
|
|||
|
try checking your database, also please post what code you have so far
|
|
|