停止执行脚本?(Stop executing a script?)
我从ajax(addContent)调用php函数:
protected $output = array('success'=>0, 'message'=>'There was an error, please try again.'); public function addContent() { $imgName = $this->doSomething(); $this->doSomethingElse(); //save imageName to DB $this->output['success'] = 1; return $output; } private function doSomething() { if($imageOk){ return $imageName; } else { $this->output['message'] = 'bad response'; //how to return output? } }
我已经将这些方法简化为说明目的。
如果方法'doSomething()'的输出响应错误,我怎样才能将它从addContent方法发送回ajax? 如果输出不好,我想退出脚本而不是继续执行doSomethingElse()。
I call a php function from ajax (addContent):
protected $output = array('success'=>0, 'message'=>'There was an error, please try again.'); public function addContent() { $imgName = $this->doSomething(); $this->doSomethingElse(); //save imageName to DB $this->output['success'] = 1; return $output; } private function doSomething() { if($imageOk){ return $imageName; } else { $this->output['message'] = 'bad response'; //how to return output? } }
I have made the methods simple for illustrative purposes.
If the method 'doSomething()' has an output of bad response, how can I send this back from the addContent method to ajax? If the output is bad, I want to exit the script and not continue executing doSomethingElse().
原文:https://stackoverflow.com/questions/19450242
最满意答案
你需要演员吗? 您已经重写了displayDetails()方法以显示特定于混合的信息。 所以你应该只能调用它,运行时将确定要调用的正确方法。
Do you need to cast ? You've already overridden the displayDetails() method to display hybrid-specific info. So you should just be able to call this and the runtime will determine the correct method to call.
相关问答
更多-
多态性的实例(Practical example of Polymorphism)[2024-03-11]
检查维基百科的例子:这是非常有用的在一个高水平: class Animal: def __init__(self, name): # Constructor of the class self.name = name def talk(self): # Abstract method, defined by convention only raise NotImplementedError("Subclass must impl ... -
拯救的隐含转换: def valueOrNone[T](value: String)(implicit conv: String => T): Option[T] = { if (!value.equals("NA")) Option(conv(value)) else None } 这适用于您声明的所有情况。 如果要转换为范围内没有默认隐式转换的某些非标准类型,请确保提供一个: class MyCustomClass(str: String) { override def toString = ...
-
如果你想到这个词的希腊根源,应该是显而易见的。 Poly = many:polygon = many-sided,polystyrene = many styrenes (a) ,polyglot = many languages等等。 变形=变化或形式:形态=研究生物形式,Morpheus =希腊神的梦想能够采取任何形式。 因此,多态性是在编程中为不同的底层表单(数据类型)呈现同一个接口的能力。 例如,整数和浮点是隐式多态的,因为您可以添加,减法,乘法等等,而不管类型是否不同。 它们在通常的术语中很少被认 ...
-
Java多态性转换 - 运行时错误ClassCastException(Java polymorphism casting - runtime error ClassCastException)[2022-02-22]
VGR的评论是正确的,但让我详细说明你的具体情况。 狗是动物,但动物不一定是狗。 您可以创建Dog对象并将它们作为Animal对象传递,但不是相反。 处理此问题的常用方法如下: public static void main(String args[]) { Dog dog = new Dog(); makeMeTalk(dog); } public static void makeMeTalk(Animal animal) { animal.talk(); } ... -
它来自希腊语“聚”(许多)和“morphe”(形式)。 一个多态对象可以采用多种形式(它可以用指向任何祖先类的指针表示)。 多态函数也可以采用多种形式(可以对实际上是不同类型的对象进行操作)。 It comes from the greek roots "poly" (many) and "morphe" (form). A polymorphic object can take on many forms (it can be represented by a pointer to any of its ...
-
Obj属于A级 所以首先你的铸造Obj是A到A,这就是为什么你得到As方法 第二次将超类A(A)转换为子类B. A不知道B Obj is of class A So first your casting Obj which is A to an A so that is why you get As methods The second time you are casting a superclass which is A to a subclass B. A doesn't know about B
-
这不是多态的损失。 这是缺乏鸭子打字 。 当静态编译器只知道它是一个Student它不允许你调用非Student方法。 即使你知道,在运行时,它将可用java的静态分类系统说不。 其他语言有鸭子打字,不关心。 Java不是其中之一。 This is not a loss of polymorphism. This is a lack of duck typing. When the compiler, statically, only knows that it's a Student it wont al ...
-
实例与铸造,多态性(Instanceof and Casting, Polymorphism)[2023-06-12]
你需要演员吗? 您已经重写了displayDetails()方法以显示特定于混合的信息。 所以你应该只能调用它,运行时将确定要调用的正确方法。 Do you need to cast ? You've already overridden the displayDetails() method to display hybrid-specific info. So you should just be able to call this and the runtime will determine the ... -
如何通过使用多态来避免'If cascade'和类型转换?(How avoid 'If cascade' and type casting by using polymorphism?)[2023-10-19]
如果您使用的是.NET 4,则可能存在重载和动态类型,这可能是您的替代方案。 class Program { static void Main(string[] args) { DC_A dca = new DC_A(); DC_B dcb = new DC_B(); XY xy = new XY(); xy.goo(dca); xy.goo(dcb); } } // base class public ... -
数组和多态(Arrays and polymorphism)[2022-03-20]
我不确定你对哪一部分感到困惑: 该数组必须是一系列书籍。 百科全书是一种书。 因此,一系列书籍可以包括任何类型的书籍 ,包括百科全书。 相反,一系列百科全书不能包含任何旧书,因为我们希望它们都是具有卷属性的百科全书。 第三本书是一本百科全书,你就投了它。 您可以在((Encyclopedia)books[2]).Volume = 10;的行中看到这一点((Encyclopedia)books[2]).Volume = 10; 。 这是允许的,但不一定鼓励,因为操作可能在运行时失败。 想象一下,如果你写过这个 ...