kingless Posted July 12, 2006 at 02:51 PM Report #37805 Posted July 12, 2006 at 02:51 PM Estou a tentar criar uma função para o meu CMS mas só que estou com problemas, o que estou a tentar fazer é criar uma função que defina uma variavel com o seu respectivo valor e outra função que vai buscar a variavel ja definida e que retorne o valor dela. O código que eu criei foi o seguinte: class lag { var $var = array(); function SetVar( $var, $valor ) { if(isset($var) AND isset($valor)) { $this->var[$var] = $valor; } else { return false; } } function GetVar( $var ) { if(isset($this->var[$var])) { return $this->var[$var]; } else { return false; } } } E para definir e ir buscar uma variavel definida era só fazer o seguinte: lag::SetVar( "teste", "isto é um teste" ); //definiu a variavel teste com o valor isto é um teste; echo lag::GetVar( "teste" ); //Buscar a variavel teste que foi definida em cima //Isto era suposto mostrar "Isto é um teste" mas não mostra nada Mas só que não funciona... Será que alguem pode me ajudar ? ?
M6 Posted July 12, 2006 at 04:15 PM Report #37820 Posted July 12, 2006 at 04:15 PM Não estás a instanciar. Ou seja, não estás a criar nenhum objecto dessa classe. Experimenta: l = new lag() l->SetVar( "teste", "isto é um teste" ); echo l->GetVar( "teste" ); O que estás a fazer é possível apenas se isso for estático, mas não sei se o modelo de objectos do PHP tem essa funcionalidade. 10 REM Generation 48K! 20 INPUT "URL:", A$ 30 IF A$(1 TO 4) = "HTTP" THEN PRINT "400 Bad Request": GOTO 50 40 PRINT "404 Not Found" 50 PRINT "./M6 @ Portugal a Programar."
kingless Posted July 12, 2006 at 04:55 PM Author Report #37826 Posted July 12, 2006 at 04:55 PM Eu já tinha utilizado desta forma.... mas só que eu quero utilizar o operador de resoluão de escopo :down:
M6 Posted July 12, 2006 at 05:02 PM Report #37828 Posted July 12, 2006 at 05:02 PM Parece-me que queres apenas usar duas funções de acesso a uma variável global, é isso? Se assim é, então não necessitas de OO para nada... Viste se o PHP objectos/métodos "static"? 10 REM Generation 48K! 20 INPUT "URL:", A$ 30 IF A$(1 TO 4) = "HTTP" THEN PRINT "400 Bad Request": GOTO 50 40 PRINT "404 Not Found" 50 PRINT "./M6 @ Portugal a Programar."
kingless Posted July 12, 2006 at 05:05 PM Author Report #37829 Posted July 12, 2006 at 05:05 PM Só queria utilizar o "::" porque acho que é mais bonito 😄 Mas parece que vou ter que utilizar mesmo a forma default :down:
M6 Posted July 13, 2006 at 09:14 AM Report #37977 Posted July 13, 2006 at 09:14 AM Não há uma forma "default", o que há é métodos dinâmicos ou estáticos. O OO não foi criado pela sua beleza! 😄 10 REM Generation 48K! 20 INPUT "URL:", A$ 30 IF A$(1 TO 4) = "HTTP" THEN PRINT "400 Bad Request": GOTO 50 40 PRINT "404 Not Found" 50 PRINT "./M6 @ Portugal a Programar."
kingless Posted July 13, 2006 at 10:15 AM Author Report #37988 Posted July 13, 2006 at 10:15 AM Eu utilizo "::" porque acho mais bonito.. eu não disse que ele foi criado pela sua beleza entendeste mal... e quando digo metodo default quer dizer o primeiro metodo que nós aprendemos a utilizar que é este : $this = new class; $this->var( "...." ); echo $this->var( "...." ); 😄 Cumps 8)
M6 Posted July 13, 2006 at 11:06 AM Report #38000 Posted July 13, 2006 at 11:06 AM Eu utilizo "::" porque acho mais bonito.. eu não disse que ele foi criado pela sua beleza entendeste mal... e quando digo metodo default quer dizer o primeiro metodo que nós aprendemos a utilizar que é este : $this = new class; $this->var( "...." ); echo $this->var( "...." ); 😄 Cumps 8) Agora sou eu que tenho uma dúvida: o PHP tem duas sintaxes para invocar um método de um objecto? 10 REM Generation 48K! 20 INPUT "URL:", A$ 30 IF A$(1 TO 4) = "HTTP" THEN PRINT "400 Bad Request": GOTO 50 40 PRINT "404 Not Found" 50 PRINT "./M6 @ Portugal a Programar."
kingless Posted July 13, 2006 at 11:18 AM Author Report #38003 Posted July 13, 2006 at 11:18 AM NÂO... mas... class a { function b ($var) { echo "$var"; } } Tanto posso chamar essa classe assim: $this = new a; $this->b("abc"); Como assim: a::b("abc"); Cumps 8)
M6 Posted July 13, 2006 at 12:48 PM Report #38032 Posted July 13, 2006 at 12:48 PM NÂO... mas... class a { function b ($var) { echo "$var"; } } Tanto posso chamar essa classe assim: $this = new a; $this->b("abc"); Como assim: a::b("abc"); Cumps 8) Não??? Mas o exemplo que dás diz que sim... Cada vez compreendo menos... A não ser que, pelo facto do método não fazer referência a nenhuma variável da classe o PHP o considerar estático. 10 REM Generation 48K! 20 INPUT "URL:", A$ 30 IF A$(1 TO 4) = "HTTP" THEN PRINT "400 Bad Request": GOTO 50 40 PRINT "404 Not Found" 50 PRINT "./M6 @ Portugal a Programar."
Guest id194 Posted July 13, 2006 at 12:54 PM Report #38033 Posted July 13, 2006 at 12:54 PM Arranjei maneira de fazer o que querias, mas pelo que me disseram isto tem os seus contras que ainda estou a tentar perceber melhor e depois posto aqui... class Foo { function Set($prop, $value) { Foo::Prop('set', $prop, $value); } function Get($prop) { return Foo::Prop('get', $prop); } function Prop($oper, $name, $value = null) { static $props = array(); if ($oper == 'set') { $props[$name] = $value; } else { if (isset($props[$name])) { return $props[$name]; } else { return false; } } } } Foo::Set('teste', "isto é um teste"); echo Foo::Get('teste');
M6 Posted July 13, 2006 at 12:56 PM Report #38034 Posted July 13, 2006 at 12:56 PM Já percebi. Estive a ler a documentação do PHP e tens de fazer a invocação com ->. O :: é para invocações de métodos estáticos ou não instanciados. "The $this pseudo-variable is not usually defined if the method in which it is hosted is called statically. This is not, however, a strict rule: $this is defined if a method is called statically from within another object. In this case, the value of $this is that of the calling object. This is illustrated in the following example:" Esta informação está aqui: http://www.php.net/manual/en/language.oop.php 10 REM Generation 48K! 20 INPUT "URL:", A$ 30 IF A$(1 TO 4) = "HTTP" THEN PRINT "400 Bad Request": GOTO 50 40 PRINT "404 Not Found" 50 PRINT "./M6 @ Portugal a Programar."
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now